473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
2 3472
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
0
by: Sugapablo | last post by:
I have two ASP pages. They basically query a database, and spit out the information as plain text in CSV format. The first has the SQL query hardcoded into it. The second takes a SQL query...
2
by: Mark | last post by:
Hi, I'm running visual studio 2003, and am now receiving the error "The Operation Could Not Be Completed" when attempting to add a form / control or component class to a project. I can add any...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
2
by: FrzzMan | last post by:
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...
2
by: SammyBar | last post by:
Hi all, I'm trying to send a message from ASP.NET to another PC by using MSMQ. I created my ASP.NET project by using Visual Studio 2005 but I initially set the project to be located on the File...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
2
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.