My C# code detects changes in files and folders attributes, using the two following snippets:
// For files
return (IO.File.GetAttributes(Source) != IO.File.GetAttributes(Dest))
// For folders
IO.DirectoryInfo SourceInfo = new IO.DirectoryInfo(AbsSource);
IO.DirectoryInfo DestInfo = new IO.DirectoryInfo(AbsDest);
return (SourceInfo.Attributes != DestInfo.Attributes);
I noticed that IO.File.GetAttributes seems to work folder folders just as well, and so I was wondering whether I could drop the directory-specific part and just use the one-liner for both files and folders.
Is that possible ? Is reading IO.DirectoryInfo.Attributes equivalent to calling File.GetAttributes?
Thanks!
As Hans Passant already pointed out in his answer it is the same thing. The first inidcation is the common base class
FileSystemInfo(Attributesproperty) thatFileInfoandDirectoryInfoshare.But is
File.GetAttributes()andDirectoryInfo.Attributesalso the same thing?To answer that question have a look at the .Net Framework reference source code.
You can download the reference source code from here .Net Reference Source Code. There you will find that both (
DirectoryInfo.Attributes) andFile.GetAttributes()both end up calling a internal function on the File class to get the attributes.Hope, this helps.