该文件正在被另一个进程使用,我必须关闭它吗?如何?
我正在使用一个文本文件来保存一些数据以供以后使用。所以我所做的就是检查文件是否存在,如果不存在,我会在需要时创建一个新文件。这给了我错误,说我的文件仍在被不同的进程使用,但我不确定为什么会这样。
我就是这样做的。在这里,我正在检查程序运行时启动的文件是否存在:
private void CreateLastOpenFile()
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (!File.Exists(file))
{
File.Create(file);
}
}
现在,我在检查或创建新文件时向其中添加一些数据(我在程序中的两个地方都有这个):
CreateLastOpenFile();
File.WriteAllText(file, data);
这里可能出了什么问题?我已经从网上阅读了一些示例,但没有看到任何有关关闭任何文件的内容。
I am using a text file to have some data there for later purposes. So what I do is to check if file exists, if not I am creating a new file when I need. This gives me error saying that my file is still being used by a different process, but I'm not sure why that is.
This is how I do it. Here, I am checking if file exists which starts when program runs:
private void CreateLastOpenFile()
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (!File.Exists(file))
{
File.Create(file);
}
}
Now, I am adding some data to it while checking or creating a new file (I am having this in 2 places in my program):
CreateLastOpenFile();
File.WriteAllText(file, data);
What could be wrong here? I have read some examples from the Net, but didn't see anything about closing any files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个。这将在文件创建期间关闭打开的流
Try this. This will close the opened stream during file creation
File.Create 正在创建一个锁定文件的 FileStream。你应该关闭它。实际上,您甚至不需要创建文件。 File.WriteAlltext 会为你做这件事
File.Create is creating a FileStream that locks the file. You should close it. Actually, you don't even need to create a file. File.WriteAlltext will do it for you
您没有关闭
File.Create()
返回的流句柄。建议您不要在您的情况下使用
File.Create()
。您可以只使用 File.WriteAllText(file, data); - 根据 MSDN 文档,如果文件不存在,它会创建文件,或者在文件存在时覆盖内容。之后关闭文件流。You are not closing the stream handle that
File.Create()
returns.Suggest you do not use
File.Create()
in your case. You can just useFile.WriteAllText(file, data);
- according to MSDN documentation it creates the file if it doesn't exist or overwrites the contents when file exists. After that closes the file stream.我建议您一步创建并填充文件数据,使用像 StreamWriter 这样允许您处置该类的类,这样做应该不会有问题,这里是一个示例:
//Doing the close and Dispose您确定文件不再被锁定
您还可以使用 File.WriteAllText(string Path, string Data),此方法不会锁定文件。
I recommend you to create and fill with data the file in one step, using some class like StreamWriter that allows you to dispose the class, you should not have problem doing it this way, here is an example:
//Doing the close and Dispose you get sure the file is not locked anymore
You can also use File.WriteAllText(string Path, string Data), this method does not lock the file.
如果您使用以下方法将数据写入文本文件,则不需要检查文件是否存在,如果不存在则创建它。 “WriteAllText”自己处理所有这些事情。如果文件不存在,它将创建文件,写入数据并关闭它,或者如果文件已存在,则覆盖文件。
File.WriteAllText(文件,数据);
If you are using below method to write the data into text file, you dont need to check if file exists and if not create it. "WriteAllText" takes cares of all these things by itself. It will create the file if not exists, write the data and close it, or overwrite the file if already exists.
File.WriteAllText(file, data);
如果您使用 writeAllText() 或 readAllText() 方法,则不使用 close() 方法,因为它们在读取或写入后关闭文件(上述方法)
if you are using writeAllText() or readAllText() method than close() method is not used as they closed file after reading or writing(above methods)