【发布时间】:2013-08-25 01:23:24
【问题描述】:
我一直在研究记事本的克隆,但遇到了问题。 当我尝试将文本框中的文本写入我创建的文件时,出现异常:
进程无法访问文件'C:\Users\opeyemi\Documents\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()锁定文件的问题。
标签: c# text-editor streamwriter