c#中一段代码使用的close进程
Close process used by a piece of code in c#
我有这个代码
path = textBox1.Text;
dir = @"C:\htmlcsseditor\" + path + ".html";
System.IO.File.Create(dir);
但是当我尝试在文件上写入时,调试告诉我该文件被另一个进程使用;我怎样才能关闭使用该文件的进程?
谢谢
The FileStream object created by this method has a default FileShare
value of None; no other process or code can access the created file
until the original file handle is closed.
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);
}
在这里你应该如何创建文件并在文件中写入一些文本。当您离开 using 块时,您正在关闭该过程。在使用结束时调用 Dispose()
释放资源的方法。
您应该处理您的文件,因为它保持打开状态。
path = textBox1.Text;
dir = @"C:\htmlcsseditor\" + path + ".html";
using (System.IO.File.Create(dir)) {} // or System.IO.File.Create(dir).Dispose()
我有这个代码
path = textBox1.Text;
dir = @"C:\htmlcsseditor\" + path + ".html";
System.IO.File.Create(dir);
但是当我尝试在文件上写入时,调试告诉我该文件被另一个进程使用;我怎样才能关闭使用该文件的进程? 谢谢
The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.
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);
}
在这里你应该如何创建文件并在文件中写入一些文本。当您离开 using 块时,您正在关闭该过程。在使用结束时调用 Dispose()
释放资源的方法。
您应该处理您的文件,因为它保持打开状态。
path = textBox1.Text;
dir = @"C:\htmlcsseditor\" + path + ".html";
using (System.IO.File.Create(dir)) {} // or System.IO.File.Create(dir).Dispose()