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 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
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
"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
"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
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
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
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
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
"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
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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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)
{...
|
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:...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |