Working with file attributes in C # .Net 2.0

So, how can I recursively search for a folder and hide ALL files and subfolders in a directory? As if he is checking every file and every folder ... if they are hidden .. open them. Iv fiddled with him all morning with no luck ... I have all the folders to get back to normal, but that’s about it.

+3
source share
3 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 this recursively use

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

to include directories use GetFileSystemEntries

Directory.GetFileSystemEntries(@"C:\Temp2", "*", SearchOption.AllDirectories)
+4
source

How about something like that?

foreach (var file in directory.GetFiles())
{
    if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden)
    {
         File.SetAttributes(file.FullName, FileAttributes.Normal);
    }
}
0
source

.

, "", -. , FileInfo DirectoryInfo .

// startDir assumed to be full path
public static void UnhideAll(string startDir)
{
    DirectoryInfo dir = new DirectoryInfo(startDir);
    Console.WriteLine("Working in {0}", startDir);
    // First, clear hidden flag on the current directory (if needed)
    if ((dir.Attributes & FileAttributes.Hidden) != 0)
    {
        Console.WriteLine("Clearing hidden flag on dir");
        dir.Attributes &= ~FileAttributes.Hidden;
    }
    else
        Console.WriteLine("No need to clear flag since it already non-hidden");

    // Second, recursively go into all sub directories
    foreach (var subDir in dir.GetDirectories())
        UnhideAll(subDir.FullName);

    // Third, fix all hidden files in the current folder
    foreach (var file in dir.GetFiles())
    {
        if ((file.Attributes & FileAttributes.Hidden) != 0)
        {
            Console.WriteLine("Clearing hidden flag on file {0}", file.FullName);
            file.Attributes &= ~FileAttributes.Hidden;
        }
        else
            Console.WriteLine("Skipping {0} since it not hidden", file.FullName);
    }
}    

,

UnhideAll(@"C:\SomePath\That\Should\Be\Unhidden");

Of course, you will want to delete all the calls Console.WriteLinewhen using this code, but I left them there to make it easier for you to see what happens when the code works. Here's a more condensed version:

// startDir assumed to be full path
public static void UnhideAll(string startDir)
{
    DirectoryInfo dir = new DirectoryInfo(startDir);
    // First, clear the current directory
    dir.Attributes &= ~FileAttributes.Hidden;
    // Second, recursively go into all sub directories
    foreach (var subDir in dir.GetDirectories())
        UnhideAll(subDir.FullName);

    // Third, fix all hidden files in this folder
    foreach (var file in dir.GetFiles())
        file.Attributes &= ~FileAttributes.Hidden;
}    
0
source