1

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();

But still occurs the same error.

Is there anything that can help me, please?

Thanks.

user3701483
  • 15
  • 1
  • 3

2 Answers2

2

Any of the writers (XmlWriter, FileWriter, etc.) should be wrapped in a using block so they are disposed of properly:

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; which may be necessary according to this SO answer

Community
  • 1
  • 1
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
  • 1
    In this case, `settings.CloseOutput` will be ignored since no *Stream* or *TextWriter* object is being passed to *XmlWriter.Create()* -- passing a file path causes the internal stream being closed when *XmlWriter* is being closed. Still, i think the remark about `settings.CloseOutput` should be kept in your answer as it is a valuable reminder regarding general usage of *XmlWriter*. –  Jun 07 '14 at 05:21
1

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 . So, to close or exit tht process, alws use using block in C# , using block alws free the resources u r using in your project

Thank you

Friyank
  • 469
  • 3
  • 8