By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,518 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

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

P: n/a
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();
}
----------------------------------------------
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jax
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
Nov 16 '05 #2

P: n/a
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();
}
----------------------------------------------
Jax wrote:
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...
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.