C# exception. File is being used by another process

前端 未结 7 1744
北恋
北恋 2020-12-19 06:43

I\'m playing around with C# and I encountered a problem. When I try to make a new file, the program breaks and says that the file is being used by another process. It\'s pro

7条回答
  •  失恋的感觉
    2020-12-19 06:53

    File.Create returns a FileStream object which holds your file. You should use this object for further task.

    var fileStream = File.Create(NameTb.Text + ".txt");
    //... do all the writing using fileStream
    fileStream.Close();
    

    or you could do just

    var fs = File.Create(NameTb.Text + ".txt");
    fs.Close();
    TextWriter tw = new StreamWriter(NameTb.Text + ".txt"); 
    tw.WriteLine("The very first line!");
    tw.Close();
    

提交回复
热议问题