Home  >  Article  >  Backend Development  >  How to open hidden files using C#?

How to open hidden files using C#?

WBOY
WBOYforward
2023-09-06 13:33:061024browse

How to open hidden files using C#?

To open a hidden file, make it visible first. You can do this by removing the hidden attributes set on it -

FileInfo file= new FileInfo(Environment.CurrentDirectory + @"\myFile.txt");
file.Attributes &= ~FileAttributes.Hidden;

Now treat it as a normal text file and open it. Read the content -

using (StreamReader sr = new StreamReader("myFile.txt")) {
   string line;

   while ((line = sr.ReadLine()) != null) {
      Console.WriteLine(line);
   }
}

After reading, set the properties to hidden again to hide the file -

file.Attributes |= FileAttributes.Hidden;

The above is the detailed content of How to open hidden files using C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete