小编ope*_*emi的帖子

写入文件时"无法访问文件"

我一直在研究记事本的克隆,我遇到了一个问题.当我尝试将文本框中的文本写入我创建的文件时,我得到了异常:

该进程无法访问文件'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();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# text-editor streamwriter

2
推荐指数
1
解决办法
1979
查看次数

标签 统计

c# ×1

streamwriter ×1

text-editor ×1