"The process cannot access the file" when trying to write to filethe second time...

F

FrzzMan

The first time I called this function, everything went well, but the
second time I called it. An Exception thrown, do you know why?

An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file
"E:\......\bin\Debug\test.xml" because it is being used by another process.

----------------------------------------------
public void updateXMLFile()
{
string AppPath = System.Windows.Forms.Application.StartupPath;
System.Xml.XmlTextWriter XMLWriter = new
System.Xml.XmlTextWriter(AppPath + "/test.xml", System.Text.Encoding.UTF8);

XMLWriter.Formatting = System.Xml.Formatting.Indented;
XMLWriter.Indentation = 1;

/// <summary>
/// <?xml version="1.0" encoding="UTF-8"?>
/// </summary>
XMLWriter.WriteStartDocument();

/// <summary>
/// <root>
/// </summary>
XMLWriter.WriteStartElement("root");

/// <summary>
/// </root>
/// </summary>
XMLWriter.WriteEndElement();

XMLWriter.WriteEndDocument();

XMLWriter.Flush();
XMLWriter.Close();
}
----------------------------------------------
 
G

Guest

Are you accessing the file elsewhere
It sounds like your standard filestream left open exception. I used to get them all the time
Logging out and back in will fix it without having to restart. You want to use try, and finally to ensure you always close the connection
I can see that you are following the protocol in the example (by closing the XMLWriter) so i'd suggest looking elsewhere in the code, or checking the documentation on the XMLWriter object to see if you have to close it another way
I don't know much about the object myself so i'm afraid I cant be of any more use, if I have been of any. :

Hope that help

jax
 
F

FrzzMan

Origin Problem:

The first time I called this function, everything went well, but the
second time I called it. An Exception thrown, do you know why?

An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file
"E:\......\bin\Debug\test.xml" because it is being used by another process.

----------------------------------------------
public void updateXMLFile()
{
string AppPath = System.Windows.Forms.Application.StartupPath;
System.Xml.XmlTextWriter XMLWriter = new
System.Xml.XmlTextWriter(AppPath + "/test.xml", System.Text.Encoding.UTF8);

XMLWriter.Formatting = System.Xml.Formatting.Indented;
XMLWriter.Indentation = 1;

/// <summary>
/// <?xml version="1.0" encoding="UTF-8"?>
/// </summary>
XMLWriter.WriteStartDocument();

/// <summary>
/// <root>
/// </summary>
XMLWriter.WriteStartElement("root");

/// <summary>
/// </root>
/// </summary>
XMLWriter.WriteEndElement();

XMLWriter.WriteEndDocument();

XMLWriter.Flush();
XMLWriter.Close();
}
----------------------------------------------

Are you accessing the file elsewhere?
It sounds like your standard filestream left open exception. I used to get them all the time.
Logging out and back in will fix it without having to restart. You want to use try, and finally to ensure you always close the connection.
I can see that you are following the protocol in the example (by closing the XMLWriter) so i'd suggest looking elsewhere in the code, or checking the documentation on the XMLWriter object to see if you have to close it another way.
I don't know much about the object myself so i'm afraid I cant be of any more use, if I have been of any. :)

Hope that helps

jax

I've solved the problem. I changed the class which this function belong
to to a singleton (and it should be a singleton), everything work as
expected.

BTW, I still don't know why the XmlTextWriter didn't close properly...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top