DirectoryInfo and FileAttributes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to compare DirectoryInfo.Attributes to FileAttributes like so:
// result returning
// Directory | Archive
DirectoryInfo info = new DirectoryInfo(this.folderFullName);
if (info.Attributes != FileAttributes.Directory)
{
throw new FoundFileException(this.folderFullName);
}

for some reason it returns true. why?
i eventually had to add a line after the info declaration like so.
string[] nfo = info.Attributes.ToString().Split(',');

and my new test will be like:
if(nfo[0] != FileAttributes.Directory.ToString() )
{
throw new FoundFileException(this.folderFullName);
}


Why is this the case???
 
DirectoryInfo info = new DirectoryInfo(this.folderFullName);
if (info.Attributes != FileAttributes.Directory)
{
throw new FoundFileException(this.folderFullName);
}

for some reason it returns true. why?

Because FileAttributes is a Flags enum and the Attribute property can
return a combination of multiple values from that enum. So rather than
testign for equality, you can test for the Directory flag like this

if ((info.Attributes & FileAttributes.Directory) != 0)


Mattias
 
Back
Top