The DirectoryInfo.Attributes property is incorrectly documented and does not raise the FileNotFound or DirectoryNotFound exceptions but instead returns the error value from the underlying win api function, which is 0xFFFFFFFF or -1.
If a path exists but access is disallowed then an attempt to retrieve the attributes will throw an exception.
If the path does not exist then the attributes will be -1.
Examples
C:\Documents and Settings\Administrator\Desktop
This is not accessible to a standard user account
DirInfo.Exists = false
DirInfo.Attributes throws Access Denied
C:\Documents and Settings\Administrator\Desktop\NoDir
This does not exist and would not be accessible
DirInfo.Exists = false
DirInfo.Attributes = -1
Test Code (run on XP Pro SP 3)
enum ExistState { exist, notExist, inaccessible };
void Check(string name) {
DirectoryInfo di = new DirectoryInfo(name);
ExistState state = ExistState.exist;
if (!di.Exists) {
try {
if ((int)di.Attributes == -1) {
state = ExistState.notExist;
}
} catch (UnauthorizedAccessException) {
state = ExistState.inaccessible;
}
}
Console.WriteLine("{0} {1}", name, state);
}
Output:
C:\Documents and Settings\Administrator\Desktop inaccessible
C:\Documents and Settings\Administrator\Desktop\NoDir notExist
I doubt that MS will fix this failing of the Attributes property to behave as documented any time soon but having said that I'm using .NET Framework 2 and am assuming that nothing has changed in the more recent versions.
One problem that I can see in my code is that it is impossible to know whether name refers to a file or a directory if the state value is 'inaccessible'.
Alan.