can't write to newly created file - file being used by another process

D

Dica

i'm getting an error about 'the process cannot access the file becaise it is
being used by another process.' write after i've created a new file and try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here



i've tried setting oFileInfo to null hoping to release the resourece, but
it's still locked. anybody know how to get around this?



tks
 
N

Nicholas Paldino [.NET/C# MVP]

Dica,

The Create method on the FileInfo instance returns a FileStream which
has a handle to the opened file.

What you should do is use the FileStream class instead. If the file
exists, then create it, otherwise, open the file.

It appears that you want to append to the end of the file as well (not
overwrite it). In that case, I would use the return value from Create if
the file doesn't exist. If it does, then you want to call the FileStream
constructor, passing in the Append value from the FileMode enumeration.

Then, with the FileStream instance, you can pass that to your
XmlTextWriter.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Dica said:
i'm getting an error about 'the process cannot access the file becaise it is
being used by another process.' write after i've created a new file and try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here

i've tried setting oFileInfo to null hoping to release the resourece, but
it's still locked. anybody know how to get around this?

Setting a variable to null won't do anything except stop that variable
from preventing the object from being garbage collected next time the
garbage collector runs.

You should call Close on the XmlTextWriter when you're done with it,
making sure you do it in a finally block so it's called even if an
exception is thrown. It's a real shame it doesn't implement
IDisposable, to let you use a "using" statement...
 
D

Dica

Nicholas Paldino said:
Dica,

The Create method on the FileInfo instance returns a FileStream which
has a handle to the opened file.

ah, yes, that makes sense. tks for the help.
What you should do is use the FileStream class instead. If the file
exists, then create it, otherwise, open the file.

It appears that you want to append to the end of the file as well (not
overwrite it). In that case, I would use the return value from Create if
the file doesn't exist. If it does, then you want to call the FileStream
constructor, passing in the Append value from the FileMode enumeration.

Then, with the FileStream instance, you can pass that to your
XmlTextWriter.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Dica said:
i'm getting an error about 'the process cannot access the file becaise it
is
being used by another process.' write after i've created a new file and
try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here



i've tried setting oFileInfo to null hoping to release the resourece, but
it's still locked. anybody know how to get around this?



tks
 
D

Dica

Jon Skeet said:
Setting a variable to null won't do anything except stop that variable
from preventing the object from being garbage collected next time the
garbage collector runs.

You should call Close on the XmlTextWriter when you're done with it,
making sure you do it in a finally block so it's called even if an
exception is thrown.

yeah, that's how i have things set up, but didn't bother including it in the
sample code since i didn't think it relevant.

It's a real shame it doesn't implement
 
N

Nicholas Paldino [.NET/C# MVP]

Dica,

XmlTextWriter does implement IDisposable. Jon was being sarcastic.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Dica said:
Jon Skeet said:
Setting a variable to null won't do anything except stop that variable
from preventing the object from being garbage collected next time the
garbage collector runs.

You should call Close on the XmlTextWriter when you're done with it,
making sure you do it in a finally block so it's called even if an
exception is thrown.

yeah, that's how i have things set up, but didn't bother including it in
the
sample code since i didn't think it relevant.

It's a real shame it doesn't implement
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
XmlTextWriter does implement IDisposable. Jon was being sarcastic.

No, I wasn't, and it doesn't - in 1.1. Try it:

using System;
using System.Xml;
using System.Text;

public class Test
{
static void Main()
{
using (XmlTextWriter x = new XmlTextWriter("test.xml",
Encoding.UTF8))
{
}
}
}

gives the error:

Test.cs(9,9): error CS0029: Cannot implicitly convert type
'System.Xml.XmlTextWriter' to 'System.IDisposable'

It works fine for 2.0, but I suspect most people aren't using that
yet...
 
J

Jon Skeet [C# MVP]

Dica said:
yeah, that's how i have things set up, but didn't bother including it in the
sample code since i didn't think it relevant.

Given that the documentation for Close says that it closes the
underlying stream, and the problem is that *something* is holding a
handle to that file open, I'd say it's extremely relevant.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
N

Nicholas Paldino [.NET/C# MVP]

Hmm, I thought XmlTextWriter derived from TextWriter (odd that it
doesn't). Oh well, we were both right, I was referring to 2.0.
 
D

Dica

Jon Skeet said:
Given that the documentation for Close says that it closes the
underlying stream, and the problem is that *something* is holding a
handle to that file open, I'd say it's extremely relevant.

i suppose i consider the closing of the xmlTextWriter irrelevant because the
programme was crapping out when the writer first attempts to open the newly
created file (prior to any previous instances of the xmlTextWriter opening
it). i've seen and addressed this problem in other languages, but since i'm
new to c#, i thought i'd ask here. thanks to Nicholas' advice, i've been
able to solve the problem by associating a fileStream object with the
create() call and subsequently closed the stream, resolving the locked file
problem. here's a more detailed code snippet:

XmlTextWriter oXmlWriter = null;


FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;


if (!bFileExists)

{

FileStream oNewFile = oFileInfo.Create();

oNewFile.Close();

}

oFileInfo = null;

oXmlWriter = new XmlTextWriter(STR_FILE_NAME, null);

try

{

oXmlWriter.Formatting = Formatting.Indented;

oXmlWriter.Indentation= 6;

oXmlWriter.Namespaces = false;

oXmlWriter.WriteStartDocument();

oXmlWriter.WriteStartElement("", "userDetails", "");

oXmlWriter.WriteStartElement("", "userName", "");

oXmlWriter.WriteString(sUserName);

oXmlWriter.WriteEndElement();

oXmlWriter.WriteStartElement("", "password", "");

oXmlWriter.WriteString(sPassword);

oXmlWriter.WriteEndElement();

oXmlWriter.WriteEndElement();

oXmlWriter.Flush();

}

catch(Exception err)

{

Console.WriteLine("Exception: {0}", err.ToString());

}

finally

{

if (oXmlWriter != null)

{

oXmlWriter.Close();

}

}

MessageBox.Show("User details saved.", "Success");
 
J

Jon Skeet [C# MVP]

Dica said:
i suppose i consider the closing of the xmlTextWriter irrelevant because the
programme was crapping out when the writer first attempts to open the newly
created file (prior to any previous instances of the xmlTextWriter opening
it).

Ah - I hadn't noticed that you were creating the file first.
i've seen and addressed this problem in other languages, but since i'm
new to c#, i thought i'd ask here. thanks to Nicholas' advice, i've been
able to solve the problem by associating a fileStream object with the
create() call and subsequently closed the stream, resolving the locked file
problem. here's a more detailed code snippet:

<snip>

That's not the way to go (in terms of elegance) - nor is it what Nick
suggested. Instead, you should use the FileStream returned by
FileInfo.Create, and pass that to the constructor for XmlTextWriter.
Then you can use a using statement:

using (FileStream stream = oFileInfo.Create())
{
XmlTextWriter writer = new XmlTextWriter (stream);
...
}

That way you only open the file once, write to it, then close it
automatically
whatever happens.

Jon
 
D

Dica

Jon Skeet said:
Ah - I hadn't noticed that you were creating the file first.


<snip>

That's not the way to go (in terms of elegance) - nor is it what Nick
suggested. Instead, you should use the FileStream returned by
FileInfo.Create, and pass that to the constructor for XmlTextWriter.
Then you can use a using statement:

using (FileStream stream = oFileInfo.Create())
{
XmlTextWriter writer = new XmlTextWriter (stream);
...
}

That way you only open the file once, write to it, then close it
automatically
whatever happens.

okay, i see where you're going with this now.

tks for the advice.
 

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