问题描述
我使用的TextWriter尝试写一个隐藏的文件,它抛出一个异常。我似乎无法弄清楚如何写一个隐藏的文件。
I am using the TextWriter to try to write to a hidden file, and it is throwing an exception. I can't seem to figure out how to write to a hidden file.
using (TextWriter tw = new StreamWriter(filename))
{
tw.WriteLine("foo");
tw.Close();
}
例外:
Exception:
Unhandled Exception: System.UnauthorizedAccessException:
Access to the path 'E:\*\media\Photos\2006-08\.picasa.ini' is denied.
我怎么能写一个隐藏的文件?
How can I write to a hidden file?
推荐答案
编辑2:这个答案解决问题,而不是处理问题的正确方法。你应该寻找卢塞罗的回答。
EDIT 2: This answer solve the problem, but is not the correct way to deal with the problem. You should look for Lucero's answer.
把这个回答:的 http://www.dotnetspark.com/Forum/314-accessing-hidden-files-and-write-it.aspx
1 - 设置文件为可见,因此它可以被覆盖
1- Set File as Visible so it can be overwritten
// Get file info
FileInfo myFile= new FileInfo(Environment.CurrentDirectory + @"\hiddenFile.txt");
// Remove the hidden attribute of the file
myFile.Attributes &= ~FileAttributes.Hidden;
2 - 进行更改文件
2- Make changes to the file
// Do foo...
3折戟文件为隐藏
// Put it back as hidden
myFile.Attributes |= FileAttributes.Hidden;
编辑:我固定在我的回答一些问题,通过briler
I fixed some problem on my answer as mentionned by briler
这篇关于我怎样写一个隐藏的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!