如何写入隐藏文件?

我正在使用TextWriter尝试写入隐藏文件,并抛出exception。 我似乎无法弄清楚如何写入隐藏文件。

using (TextWriter tw = new StreamWriter(filename)) { tw.WriteLine("foo"); tw.Close(); } 

例外:

 Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'E:\*\media\Photos\2006-08\.picasa.ini' is denied. 

如何写入隐藏文件?

编辑2:这个答案解决了问题,但不是解决问题的正确方法。 你应该寻找Lucero的答案。


从以下url获取此答案: http : //www.dotnetspark.com/Forum/314-accessing-hidden-files-and-write-it.aspx

1-将文件设置为可见,以便可以覆盖它

 // Get file info FileInfo myFile= new FileInfo(Environment.CurrentDirectory + @"\hiddenFile.txt"); // Remove the hidden attribute of the file myFile.Attributes &= ~FileAttributes.Hidden; 

2-更改文件

 // Do foo... 

3-将文件设置为隐藏

 // Put it back as hidden myFile.Attributes |= FileAttributes.Hidden; 

编辑:我修正了一些问题,我的回答是由briler提到的

似乎问题是File.Exists()检查是在内部完成的,如果文件被隐藏则会失败(例如,尝试对已经存在的文件执行FileMode.Create )。

因此,使用FileMode.OpenOrCreate确保文件被打开或创建,即使它被隐藏,或者只是FileMode.Open如果你不想创建它,如果它不存在。

但是,当使用FileMode.OpenOrCreate ,文件不会被截断,因此您应该在结尾处设置其长度,以确保在文本结束后没有剩余文件。

 using (FileStream fs = new FileStream(filename, FileMode.Open)) { using (TextWriter tw = new StreamWriter(fs)) { // Write your data here... tw.WriteLine("foo"); // Flush the writer in order to get a correct stream position for truncating tw.Flush(); // Set the stream length to the current position in order to truncate leftover text fs.SetLength(fs.Position); } } 

如果您使用的是.NET 4.5或更高版本,则会出现一个新的重载,它会阻止StreamWriter处理底层流。 然后可以更直观地编写代码,如下所示:

 using (FileStream fs = new FileStream(filename, FileMode.Open)) { using (TextWriter tw = new StreamWriter(fs, Encoding.UTF8, 1024, true)) { // Write your data here... tw.WriteLine("foo"); } // Set the stream length to the current position in order to truncate leftover text fs.SetLength(fs.Position); } 

编辑:Pierre-Luc Champigny回答 inccorect,但现在按照我的说法修复,我将它留作参考

 myFile.Attributes |= FileAttributes.Normal; 

不会从文件中删除隐藏属性。 为了删除unhidden属性使用:

 FileInfo .Attributes &= ~FileAttributes.Hidden; 

此代码检查文件是否存在,使其取消隐藏。 在写完一次之前,它再次将其设置为隐藏。 我也设置了普通属性,以防不存在 – 你不必使用它

 // if do not exists it creates it. FileInfo FileInfo = new FileInfo(FileName); if (true == FileInfo .Exists) { // remove the hidden attribute from the file FileInfo .Attributes &= ~FileAttributes.Hidden; } //if it doesn't exist StreamWriter will create it using (StreamWriter fileWriter = new StreamWriter(FileName)) { fileWriter.WriteLine("Write something"); } // set the file as hidden FileInfo.Attributes |= FileAttributes.Hidden; 

如果这是您的选项,您可以尝试使用File.SetAttributes暂时删除隐藏属性,执行您的工作,然后将其设置回以前的状态。

您可以在写入文件之前取消隐藏文件,完成后再将其隐藏起来。

打开文件后,可以更改其属性(包括只读)并继续写入文件。 这是防止文件被其他进程覆盖的一种方法。

因此,似乎可以取消隐藏文件,打开它,然后将其重置为隐藏,即使您已打开它。

例如,以下代码有效:

 public void WriteToHiddenFile(string fname) { TextWriter outf; FileInfo info; info = new FileInfo(fname); info.Attributes = FileAttributes.Normal; // Set file to unhidden outf = new StreamWriter(fname); // Open file for writing info.Attributes = FileAttributes.Hidden; // Set back to hidden outf.WriteLine("test output."); // Write to file outf.Close(); // Close file } 

请注意,在进程写入文件时,该文件仍保持隐藏状态。