我一直在努力克隆記事本,但遇到了問題。 當我嘗試在文本框中的文本寫入其中我創建了一個文件,我得到異常:寫入文件時出現「無法訪問文件」
該進程無法訪問文件「C:\用戶\ opeyemi \文檔\ b.txt」 因爲它正在被另一個進程使用。
下面是我寫的代碼。我非常感謝任何有關我下一步應該做什麼的建議。
private void Button_Click_1(object sender, RoutedEventArgs e)
{
SaveFileDialog TextFile = new SaveFileDialog();
TextFile.ShowDialog();
// this is the path of the file i wish to save
string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),TextFile.FileName+".txt");
if (!System.IO.File.Exists(path))
{
System.IO.File.Create(path);
// i am trying to write the content of my textbox to the file i created
System.IO.StreamWriter textWriter = new System.IO.StreamWriter(path);
textWriter.Write(textEditor.Text);
textWriter.Close();
}
}
_「我真的很感激的是我應該做的下一任何意見」 _ - 無需在網上搜索時收到錯誤不開放的問題。在這裏每兩天詢問一個關於'File.Create()'鎖定文件的問題。 – CodeCaster
[File正在被其他進程使用File.Create()]後可能的重複(http://stackoverflow.com/questions/2781357/file-being-used-by-another-process-after-using-file-創建) – CodeCaster