System.IO.File.Create锁定文件 [英] System.IO.File.Create locking a file
问题描述
正如问题说...我使用System.IO.File.Create创建文件。我不是书面方式将其与流作家,只是创造它。
Just as the question says... I'm using System.IO.File.Create to create a file. I'm not writting to it with a stream writer, just creating it.
我得到在前端服务器错误时,应用程序改掉打开新创建的文件 - 该文件正在使用中。那么垃圾收集似乎一起去,几分钟后一切正常。
I get a server error in the front end when the app trys to open the newly created file - that the file is in use. Garbage collection then seems to come along and a few minutes later all is OK.
现在我知道,如果我使用的StreamWriter我将不得不关闭它。做同样适用于创建?
Now I know if I was using Streamwriter I would have to close it. Does the same apply to creating?
我读过,打开一个流作家的文件,然后立即关闭它会解决这个问题,但似乎混乱。有没有更简单的方法吗?
I've read that opening a stream writer to the file then immediately closing it will fix this but it seems messy. Is there a simpler way?
谢谢!
推荐答案
File.Create
返回的FileStream
。你应该使用这样的:
File.Create
returns a FileStream
. You should use it like this:
using (FileStream fs = File.Create(path))
{
//you can use the filstream here to put stuff in the file if you want to
}
这篇关于System.IO.File.Create锁定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!