2010-02-11 43 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