在C#中,使用System.IO.File.Create()
创建完一个文件之后,如果需要对这个文件进行写操作,会出现错误,提示你“这个文件正在被使用”。原因是System.IO.File.Create()
返回的是一个FileStream
,这个需要关闭,才能对其创建的文件进行写操作。有两种方法:
直接关闭,像这样:
System.IO.File.Create("the path of the file").Close();
-
使用
using
自动关闭:using(System.IO.File.Create("the path of the file"))
{
//Can do something else or nothing
}
注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版权等事宜,请你联系站长进行处理。