C# 进程无法访问文件';C:\file.xml';因为它正被另一个进程使用 DirectoryInfo dir=新的DirectoryInfo(路径); 如果(!dir.存在) { CreateDirectory(路径); 创建(路径+“\\File.xml”); StreamWriter sw=新的StreamWriter(路径+“\\file.xml”); sw.Flush(); sw.写(“”); }

C# 进程无法访问文件';C:\file.xml';因为它正被另一个进程使用 DirectoryInfo dir=新的DirectoryInfo(路径); 如果(!dir.存在) { CreateDirectory(路径); 创建(路径+“\\File.xml”); StreamWriter sw=新的StreamWriter(路径+“\\file.xml”); sw.Flush(); sw.写(“”); },c#,c#-4.0,file-io,C#,C# 4.0,File Io,错误: 进程无法访问文件“C:\file.xml”,因为它正在被删除 被另一个进程使用 为什么??如何关闭文件?使用文件。创建或StreamWriter,而不是两者都使用文件。创建或StreamWriter,而不是两者都使用sw.close()代码 您最好使用XmlDocument。然后可以附加节点,如节点等 XmlDocument具有内置的保存功能,因此您不必像streamwriter那样管理这些功能。Dosw.Close()代码 您最好使用XmlDocument。然后可以附加节点,如节点等

错误:

进程无法访问文件“C:\file.xml”,因为它正在被删除 被另一个进程使用


为什么??如何关闭文件?

使用
文件。创建
StreamWriter
,而不是两者都使用
文件。创建
StreamWriter
,而不是两者都使用
sw.close()Write()后编写>代码

您最好使用XmlDocument。然后可以附加节点,如节点等

XmlDocument具有内置的保存功能,因此您不必像streamwriter那样管理这些功能。

Do
sw.Close()Write()后编写>代码

您最好使用XmlDocument。然后可以附加节点,如节点等

XmlDocument具有内置的保存功能,因此您不必像streamwriter那样管理这些功能。

此方法创建的FileStream对象(File.Create)的默认FileShare值为None;在关闭原始文件句柄之前,任何其他进程或代码都无法访问创建的文件

因此,解决办法是

 DirectoryInfo dir=new DirectoryInfo(path);
 if (!dir.Exists)
  {
      Directory.CreateDirectory(path);
      File.Create(path + "\\file.xml");

      StreamWriter sw =new StreamWriter(path + "\\file.xml");
      sw.Flush();
      sw.Write("<?xml version='1.0' encoding='utf-8' ?><project></project>");
  }
使用(FileStream fs=File.Create(path+“\\File.xml”))
{
Byte[]info=新的UTF8Encoding(true).GetBytes(“”);
fs.Write(信息,0,信息长度);
}
编辑:已更改删除StreamWriter的创建并使用FileStream
但是,我不喜欢MSDN建议的这种方式。
StreamWriter有一个可以获取文件流的构造函数,但我认为如果我们使用

  using(FileStream fs = File.Create(path + "\\file.xml"))
  {
       Byte[] info = new UTF8Encoding(true).GetBytes("<?xml version='1.0' encoding='utf-8' ?><project></project>");
       fs.Write(info, 0, info.Length);
  }
使用(StreamWriter sw=new StreamWriter(File.Create(path+“\\File.xml”))
{ 
sw.写(“”);
}
我们得到了锁的问题。但是,我已经测试过了,它可以正常工作。
StreamWriter构造函数可能会对File.Create返回的文件流执行一些技巧。

此方法创建的FileStream对象(File.Create)的默认FileShare值为None;在关闭原始文件句柄之前,任何其他进程或代码都无法访问创建的文件

因此,解决办法是

 DirectoryInfo dir=new DirectoryInfo(path);
 if (!dir.Exists)
  {
      Directory.CreateDirectory(path);
      File.Create(path + "\\file.xml");

      StreamWriter sw =new StreamWriter(path + "\\file.xml");
      sw.Flush();
      sw.Write("<?xml version='1.0' encoding='utf-8' ?><project></project>");
  }
使用(FileStream fs=File.Create(path+“\\File.xml”))
{
Byte[]info=新的UTF8Encoding(true).GetBytes(“”);
fs.Write(信息,0,信息长度);
}
编辑:已更改删除StreamWriter的创建并使用FileStream
但是,我不喜欢MSDN建议的这种方式。
StreamWriter有一个可以获取文件流的构造函数,但我认为如果我们使用

  using(FileStream fs = File.Create(path + "\\file.xml"))
  {
       Byte[] info = new UTF8Encoding(true).GetBytes("<?xml version='1.0' encoding='utf-8' ?><project></project>");
       fs.Write(info, 0, info.Length);
  }
使用(StreamWriter sw=new StreamWriter(File.Create(path+“\\File.xml”))
{ 
sw.写(“”);
}
我们得到了锁的问题。但是,我已经测试过了,它可以正常工作。
StreamWriter构造函数可能会对File.Create返回的FileStream执行一些操作。

更改

   using(StreamWriter sw = new StreamWriter(File.Create(path + "\\file.xml")))
   { 
      sw.Write("<?xml version='1.0' encoding='utf-8' ?><project></project>");
   }

改变


错误:无法将类型“System.IO.FileStream”隐式转换为“System.IO.StreamWriter”错误:无法将类型“System.IO.FileStream”隐式转换为“System.IO.StreamWriter”请编写一个创建并添加to@user1263390
StreamWriter sw=newstreamwriter(路径+“\\file.xml”,false)请编写一个创建然后添加的示例to@user1263390
StreamWriter sw=newstreamwriter(路径+“\\file.xml”,false)