在C#中,使用system.IO.File.Create()创建完一个文件之后,如果需要对这个文件进行写操作,会出现错误,提示你“这个文件正在被使用”。

原因是System.IO.File.Create()返回的是一个FileStream,这个需要关闭,才能对其创建的文件进行写操作。有两种方法:

1. 直接关闭,像这样:
System.IO.File.Create("the path of the file").Close();

2. 使用using自动关闭:

using(System.IO.File.Create("the path of the file"))
{
  //Can do something else or nothing
}

相关文章:

  • 2022-12-23
  • 2021-07-27
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-01-30
  • 2022-12-23
相关资源
相似解决方案