如何写入隐藏文件?

esa*_*sac 22 c# hidden file

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

using (TextWriter tw = new StreamWriter(filename))
{
    tw.WriteLine("foo");
    tw.Close();
}
Run Code Online (Sandbox Code Playgroud)

例外:

Unhandled Exception: System.UnauthorizedAccessException: 
Access to the path 'E:\*\media\Photos\2006-08\.picasa.ini' is denied.
Run Code Online (Sandbox Code Playgroud)

如何写入隐藏文件?

Luc*_*ero 42

似乎问题是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);
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是.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);
}
Run Code Online (Sandbox Code Playgroud)

  • Upvoted,因为这是正确的操作...不"取消隐藏文件,写入它,然后再次隐藏它"进程 (5认同)
  • 感谢您明确说明为什么会发生这种情况,因为我没有看到"隐藏"属性如何影响文件权限. (2认同)

Pie*_*gny 18

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


从以下网址获取答案: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;
Run Code Online (Sandbox Code Playgroud)

2-更改文件

// Do foo...
Run Code Online (Sandbox Code Playgroud)

3-将文件设置为隐藏

// Put it back as hidden
myFile.Attributes |= FileAttributes.Hidden;
Run Code Online (Sandbox Code Playgroud)

编辑:我修正了briler提到的答案

  • 取消隐藏属性,然后在写入文件后重置隐藏的内容充满了很多问题.只需使用一个正确处理此问题的FileStream重载,如@Lucero中所述. (3认同)

bri*_*ler 9

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

myFile.Attributes |= FileAttributes.Normal;
Run Code Online (Sandbox Code Playgroud)

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

FileInfo .Attributes &= ~FileAttributes.Hidden; 
Run Code Online (Sandbox Code Playgroud)

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

// 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;
Run Code Online (Sandbox Code Playgroud)