C# exception. File is being used by another process

前端 未结 7 1743
北恋
北恋 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 07:12

    As per MSDN the File.Create Method (String) uses a FileStream which in your case is not being closed. Use something like this:

    FileStream fs = new FileStream(NameTb.Text + ".txt");
    File.Create(fs);
    fs.Close();
    

    or @Muctadir Dinar

    var fileStream = File.Create(NameTb.Text + ".txt");
    //... do all the writing using fileStream
    fileStream.Close();
    
    0 讨论(0)
提交回复
热议问题