Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I safely use `IO.File.GetAttributes` on a folder?

Tags:

c#

io

attributes

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!

like image 749
Clément Avatar asked Oct 15 '25 21:10

Clément


1 Answers

Yes, same thing. You can tell from the class inheritance. The FileInfo and DirectoryInfo classes both inherit the non-abstract Attribute property from the abstract FileSystemInfo class.

like image 123
Hans Passant Avatar answered Oct 18 '25 10:10

Hans Passant