我收到"The process cannot access the file because it is being used by another process"错误。有什么想法吗?



所以,我在AppData中创建了一个文件和一个txt文件,我想覆盖txt。但是当我尝试这样做时,它不断给我这个错误。有什么想法吗?

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string setuppath = (path + "\");
string nsetuppath = (setuppath + "newx" + "\");
Directory.CreateDirectory(nsetuppath);
string hedef2 = (nsetuppath + "commands.txt");
File.Create(hedef2);
StreamWriter sw = new StreamWriter(hedef2);   ----> This is where the error appears.
sw.WriteLine("Testtest");

使用流时只需使用using语句即可。using 语句在使用对象的代码完成时自动对对象调用 Dispose。

//path to the file you want to create
string path = @"C:codeTest.txt";
// Create the file, or overwrite if the file exists.
using (FileStream fs = File.Create(path))
{
byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}

有许多操作流的方法,根据您的需要保持简单

相关内容

最新更新