473,387 Members | 1,812 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.

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

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
Nov 17 '05 #1
11 4321
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dica" <ge*****@hotmail.com> wrote in message
news:_L******************************@rogers.com.. .
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

Nov 17 '05 #2
Dica <ge*****@hotmail.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uq**************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Dica" <ge*****@hotmail.com> wrote in message
news:_L******************************@rogers.com.. .
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


Nov 17 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dica <ge*****@hotmail.com> wrote:
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.


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 IDisposable, to let you use a "using" statement...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
Dica,

XmlTextWriter does implement IDisposable. Jon was being sarcastic.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dica" <ge*****@hotmail.com> wrote in message
news:7q********************@rogers.com...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dica <ge*****@hotmail.com> wrote:
> 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.


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
IDisposable, to let you use a "using" statement...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Nov 17 '05 #6
Dica <ge*****@hotmail.com> wrote:
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Hmm, I thought XmlTextWriter derived from TextWriter (odd that it
doesn't). Oh well, we were both right, I was referring to 2.0.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dica <ge*****@hotmail.com> wrote:
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.


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



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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #10
Dica wrote:
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

Nov 17 '05 #11

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Dica wrote:
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.


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

tks for the advice.

Jon

Nov 17 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
4
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG:...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.