2010-02-11 45 views
20

我正在使用TextWriter尝试写入隐藏文件,并引发异常。我似乎无法弄清楚如何写入隐藏文件。如何写入隐藏文件?

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

什么异常? – tadman 2010-02-11 19:10:50

+0

它抛出的异常是什么? – 2010-02-11 19:11:14

+1

扔什么异常? – Seth 2010-02-11 19:11:20

回答

17

编辑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; 

2 - 进行更改文件

// Do foo... 

3 - 将文件设置为隐藏

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

编辑:我固定在我的回答一些问题,通过作为mentionned briler

+0

@Lucero我固定我的答案 – 2014-02-27 18:36:23

+3

取消设置隐藏属性,然后在您重新隐藏”已经写入该文件是充满了这么多问题。只需使用其中一个正确处理此问题的FileStream重载,如@Lucero答案中所述。 – enorl76 2014-12-19 21:51:12

+0

请参阅@Lucero的答案,不需要更改要写入隐藏文件的属性。 – SergeyT 2016-03-11 12:54:58

0

如果这是你的选择,你可以尝试使用File.SetAttributes暂时删除的隐藏属性,做你的工作,然后将其返回到以前的状态。

+0

坏的选择,因为我在另一个答复中提到 – enorl76 2014-12-19 21:51:54

0

你可以写进去后再去完成写作隐藏它后取消隐藏文件。

0

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

所以似乎可以取消隐藏文件,打开它,然后将其重置为隐藏,即使你有它打开。

例如,下面的代码工作:

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 
} 

注意,虽然过程写入它的文件保持隐藏。

+0

不好的选择。如果在取消设置隐藏属性和重置隐藏属性之间发生了某些事情,那么您已经不必要地更改了文件的状态。简单地使用FileStream ctor的重载。 – enorl76 2014-12-19 21:53:08

30

看来问题在于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, false)) { 
    // 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); 
} 
+4

Upvoted因为这是正确的操作......不是“取消隐藏文件,写微博,然后再隐藏”的过程 – enorl76 2014-12-19 22:00:10

+1

谢谢你澄清正是为什么出现这种情况,因为我没有看到一个“隐藏”属性如何影响文件权限。 – 2015-02-10 10:37:56

+0

@DaveVandenEynde,不客气。不幸的是,大多数访问这个问题的人似乎都没有意识到,来回改变属性并不是真正的解决方案,而只是一个脆弱的解决方法。 – Lucero 2015-02-10 14:51:09

9

编辑:皮埃尔 - 吕克·皮尼答案 inccorect,但现在固定根据矿井, 我身后把它当作参考

myFile.Attributes |= FileAttributes.Normal; 

不会从文件中删除隐藏属性。 为了消除取消隐藏属性,使用:如果

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; 
+1

那么我解决了我的答案的问题,对此表示歉意 – 2014-01-22 16:50:23

+0

为什么首先取消隐藏文件?您修改文件的状态。 – enorl76 2014-12-19 21:59:33

+0

因为通常会得到一个UnauthorizedAccessException,请参阅最初的问题。 – briler 2014-12-21 08:49:18