简体   繁体   English

该进程无法访问'xml'文件,因为它正由另一个进程使用

[英]The process cannot access the 'xml' file because it is being used by another process

I want to update a file. 我想更新一个文件。 I opened the file and then tried to write the content to it but got the exception: 我打开文件,然后尝试将内容写入其中,但得到了异常:

The process cannot access the file because it is being used by another process. 该进程无法访问该文件,因为该文件正由另一个进程使用。

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter writer = XmlWriter.Create("../../../connString.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("ConnectionString");

if (ConnType == "SQL Server") {
   writer.WriteAttributeString("ID", "SQL Server");
   writer.WriteAttributeString("DataSource", Convert.ToString(TEServer.EditValue));
   writer.WriteAttributeString("Database", Convert.ToString(TEDatabase.EditValue));
   writer.WriteAttributeString("UserID", Convert.ToString(TEUserID.EditValue));
   writer.WriteAttributeString("Password", Convert.ToString(TEPassword.EditValue));
} else if (ConnType == "Access") {
   writer.WriteAttributeString("ID", "Access");
   writer.WriteAttributeString("DbLocation", Convert.ToString(BtnEditDBLoc.EditValue));
}

writer.WriteEndElement();
writer.WriteEndDocument();

writer.Flush();
writer.Close();

I've been looking at some sources said to be close "XmlWriter" but in my coding i have closed, with : writer.Close(); 我一直在看一些消息来源说是接近“XmlWriter”,但在我的编码中我已经关闭了,有:writer.Close();

But still occurs the same error. 但仍然会出现相同的错误。

Is there anything that can help me, please? 请问有什么可以帮助我的吗?

Thanks. 谢谢。

Any of the writers (XmlWriter, FileWriter, etc.) should be wrapped in a using block so they are disposed of properly: 任何编写器(XmlWriter,FileWriter等)都应该包含在一个使用块中,以便正确处理它们:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.CloseOutput = true; 

using (XmlWriter writer = XmlWriter.Create("../../../connString.xml", settings)) {
   writer.WriteStartDocument();
   writer.WriteComment("This file is generated by the program.");
   writer.WriteStartElement("ConnectionString");

   if (ConnType == "SQL Server") {
      writer.WriteAttributeString("ID", "SQL Server");
      writer.WriteAttributeString("DataSource", Convert.ToString(TEServer.EditValue));
      writer.WriteAttributeString("Database", Convert.ToString(TEDatabase.EditValue));
      writer.WriteAttributeString("UserID", Convert.ToString(TEUserID.EditValue));
      writer.WriteAttributeString("Password", Convert.ToString(TEPassword.EditValue));
   } else if (ConnType == "Access") {
      writer.WriteAttributeString("ID", "Access");
      writer.WriteAttributeString("DbLocation", Convert.ToString(BtnEditDBLoc.EditValue));
   }

   writer.WriteEndElement();
   writer.WriteEndDocument();

   //writer.Flush(); //Flush isn't required--handled by the using block
   //writer.Close(); //close isn't required--handled by the using block
}

I also added settings.CloseOutput = true; 我还添加了settings.CloseOutput = true; which may be necessary according to this SO answer 根据这个SO答案可能是必要的

Use using block for accessing any file from system, as accessing any file create a process in our system, and so it neva allow same file to access by two thread . 使用块来访问系统中的任何文件,因为访问任何文件都会在我们的系统中创建一个进程,因此neva允许两个线程访问同一个文件。 So, to close or exit tht process, alws use using block in C# , using block alws free the resources ur using in your project 因此,为了关闭或退出该过程,alws在C#中使用块,使用块alws释放你在项目中使用的资源

Thank you 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “进程无法访问该文件,因为它正被另一个进程使用”与图像 - “The process cannot access the file because it is being used by another process” with Images 该进程无法访问文件“ XXX”,因为它正在被另一个进程使用 - The process cannot access the file 'XXX' because it is being used by another process 该进程无法访问文件XXX,因为它正在被另一个进程使用 - The process cannot access the file XXX because it is being used by another process SSIS - 该进程无法访问该文件,因为它正被另一个进程使用 - SSIS - The process cannot access the file because it is being used by another process DownloadFile该进程无法访问文件,因为该文件正在被另一个进程使用 - DownloadFile The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问文件“路径”,因为它正在被另一个进程使用 - The process cannot access the file 'path' because it is being used by another process “进程无法访问该文件,因为它正由另一个进程使用” - “The process cannot access the file because it is being used by another process ” 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM