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 that开发者_如何转开发s about it.
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)
How about something like this?
foreach (var file in directory.GetFiles())
{
if ((File.GetAttributes(file.FullName) & FileAttributes.Hidden) == FileAttributes.Hidden)
{
File.SetAttributes(file.FullName, FileAttributes.Normal);
}
}
This is easily solved using recursion.
File attributes such as "hidden" is specified as an enum flag and is most easliy set or cleared using bit operations. You can see a file or directory's attributes by getting a FileInfo or DirectoryInfo for that path.
// 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's 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's not hidden", file.FullName);
}
}
You can now recursively unhide all hidden files inside a directory by calling
UnhideAll(@"C:\SomePath\That\Should\Be\Unhidden");
Of course, you'll want to remove all calls to Console.WriteLine
when you use this code but I left them there to make it easier for you to see what's going on when the code is running. Here's a more condense 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;
}
精彩评论