Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with File Attributes in C# .Net 2.0

So how can i recursively search a Folder and un-hide ALL files and sub folders in a directory? Like have it check each file and each folder... if they're hidden.. un-hide them. Iv been messing around with it all morning with no luck... i got all folders to set back to normal but thats about it.

like image 473
NightsEVil Avatar asked Dec 27 '25 21:12

NightsEVil


1 Answers

foreach (var filePath in Directory.GetFiles(@"C:\Temp2"))
{
    Console.Write("File " + filePath);

    FileAttributes fileAttribute = File.GetAttributes(filePath);

    if ((fileAttribute & FileAttributes.Hidden) > 0)
    {
        Console.WriteLine(" is hidden.");

        // unset the hidden flag, but do not change other flags:
        File.SetAttributes(filePath, fileAttribute & ~FileAttributes.Hidden);
    }
    else
    {
        Console.WriteLine(" is not hidden.");
    }
}

to do it recursively, use

Directory.GetFiles(@"C:\Temp2", "*", SearchOption.AllDirectories)

to include directories too, use GetFileSystemEntries

Directory.GetFileSystemEntries(@"C:\Temp2", "*", SearchOption.AllDirectories)
like image 94
ulrichb Avatar answered Dec 30 '25 10:12

ulrichb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!