Monday, July 7, 2008

How to check for hidden/system files and folders.

Hi,

Maybe not everybody knows how to check whether file or folder is hidden/system.
This code returns array of FileInfo and DirectoryInfo objects.

FileInfo[] info = dirInfo.GetFiles();
DirectoryInfo[] dirs = dirInfo.GetDirectories();

They both have Attributes property which is flag enum. So you can use this property to check for files attributes. Here is sample method to check whether object has hidden or system attributes:

private static bool IsNotHiddenOrSystem(FileAttributes attributes)
{
return (attributes & (FileAttributes.Hidden FileAttributes.System)) == 0;
}

Full list of file attributes can be found on MSDN site:
http://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx

Enjoy ;)

No comments: