C# 如何写入隐藏文件?
我正在使用TextWriter尝试写入一个隐藏文件,但它引发了一个异常。我似乎不知道如何写入隐藏文件C# 如何写入隐藏文件?,c#,file,hidden,C#,File,Hidden,我正在使用TextWriter尝试写入一个隐藏文件,但它引发了一个异常。我似乎不知道如何写入隐藏文件 using (TextWriter tw = new StreamWriter(filename)) { tw.WriteLine("foo"); tw.Close(); } 例外情况: Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'E:\*\media\Photos\
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.
如何写入隐藏的文件?如果这是您的一个选项,您可以尝试使用
file.SetAttributes
临时删除隐藏的属性,完成工作,然后将其设置回以前的状态。编辑2:此答案解决了问题,但不是解决问题的正确方法。你应该寻找卢塞罗的答案
这个答案来自: 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提到的我的答案中的一些问题。您可以在写入文件之前取消隐藏该文件,然后在完成写入后再次隐藏该文件。打开文件后,您可以更改其属性(包括只读)并继续写入该文件。这是防止文件被其他进程覆盖的一种方法 因此,似乎可以取消隐藏文件,打开它,然后将其重置为隐藏,即使您已经打开了它 例如,以下代码起作用:
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
}
请注意,当进程写入文件时,该文件仍处于隐藏状态。问题似乎是这样的
文件。Exists()
检查是在内部完成的,如果文件处于隐藏状态,则会失败(例如,尝试对已存在的文件执行文件模式。创建)
因此,使用FileMode.Open或create
确保即使文件隐藏也已打开或创建,或者如果文件不存在,则只使用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的答案是正确的,但现在根据我的答案进行了修正,
我把它留下作为参考
myFile.Attributes |= FileAttributes.Normal;
不会从文件中删除隐藏属性。
要删除未隐藏的属性,请使用:
FileInfo .Attributes &= ~FileAttributes.Hidden;
此代码检查文件是否存在,并将其取消隐藏。写作前
完成后,再次将其设置为隐藏。
我还设置了normal属性,以防该属性不存在-您不必使用它
// 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;
它抛出的异常是什么?它抛出的异常是什么?我不知道是否有人在寻找它,但我添加了异常只是为了以防万一。我喜欢答案与注释一样冗余。@Epiless:在这个问题中,答案是高数量的,而不是高质量的…;)我解决了我答案上的问题,很抱歉,为什么首先要取消隐藏文件?您正在修改文件的状态。因为您通常会遇到UnauthorizedAccessException,请参见初始问题。@Lucero I修复了我的回答:取消隐藏属性,然后在写入文件后重置隐藏,这会带来很多问题。正如@Lucero answer中提到的那样,只需使用其中一个能够正确处理此问题的FileStream重载即可。请参阅@Lucero的答案,无需更改属性以写入隐藏文件。bad选项,正如我在另一个answerBad选项中提到的。如果在取消设置隐藏属性和重置隐藏属性之间发生了某种情况,则不必要地更改了文件的状态。简单地使用FileStream ctor.Upvoted重载,因为这是正确的操作。。。不是“取消隐藏文件,写入文件,然后再次隐藏”过程谢谢您确切地解释了为什么会发生这种情况,因为我没有看到“隐藏”属性如何影响文件权限。@DaveVandenEynde,不客气。不幸的是,大多数访问此问题的人似乎没有意识到来回更改属性并不是一个真正的解决方案,而只是一个脆弱的解决方法。tw.Close()是unnesessary@SergeyT你当然是对的,但我把它包括在答案中,因为这是OP在问题中的片段。我刚刚修改了StreamWriter的创建,以展示如何处理隐藏文件。