Mega Code Archive

 
Categories / C# / File Stream
 

File GetAttributes Method Gets the FileAttributes of the file on the path

using System; using System.IO; using System.Text; class Test  {     public static void Main()      {         string path = @"c:\MyTest.txt";         if (!File.Exists(path))          {             File.Create(path);         }         FileAttributes attributes = File.GetAttributes(path);         if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)         {             attributes = RemoveAttribute(attributes, FileAttributes.Hidden);             File.SetAttributes(path, attributes);             Console.WriteLine("The {0} file is no longer hidden.", path);         }          else          {             File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);             Console.WriteLine("The {0} file is now hidden.", path);         }     }     private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)     {         return attributes & ~attributesToRemove;     } }