如何使用 C# 開啟隱藏檔案?


要開啟隱藏檔案,首先將其設定為可見。可以透過刪除設定在其上的隱藏屬性來實現此目的 -

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

現在將其視為普通文字檔案並將其開啟。讀取內容 -

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

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

讀取後,再次將屬性設定為隱藏以隱藏該檔案 -

file.Attributes |= FileAttributes.Hidden;

更新於: 2020 年 6 月 22 日

564 次瀏覽

開啟你的 職業生涯

完成課程以獲得證書

開始
廣告