473,394 Members | 1,817 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,394 software developers and data experts.

File being used by another process

2
I'm having a very erratic behavior when I try to create a new xml file using XmlTextWriter. Sometimes it works, but most of the time it throws that exception.
It feels like the program is not letting itself access the file.

Expand|Select|Wrap|Line Numbers
  1. XmlTextWriter xt = new XmlTextWriter(filePath, null);
  2.  
filePath is c:\xmltest\test.xml

I have no idea what may be the problem, and it's very frustrating. Any kind of help will be greatly appreciated.
Jun 19 '09 #1
4 3176
tlhintoq
3,525 Expert 2GB
Is the error actually at creation, or on a following line when you try to write into the file?

What about anti-virus seeing the new file and trying to scan it?
Jun 19 '09 #2
PRR
750 Expert 512MB
You need to dispose of XmlTextwriter ( in case the error is generated after you have created the file while accessing second time). Using statement would be better
Expand|Select|Wrap|Line Numbers
  1. using(XmlTextWriter xt = new XmlTextWriter(filePath, null))
  2. {
  3. }
  4.  
Jun 22 '09 #3
Curtis Rutland
3,256 Expert 2GB
PRR's way is great. The XmlTextWriter (and all the other stream readers and writers) implement IDisposable, which provides a means to clean up after themselves.

What is happening under the covers:
When you instantiate a new XmlTexWriter with a path as a parameter, it is creating a FileStream with a FileAccess of "Write." In windows, that puts a lock on the file for editing, basically, the process that your program is on has an exclusive lock on that file.

It maintains this lock until you call the .Close() method on your stream. If you terminate your program before you release the file, the file will remain locked. Eventually the OS will unlock the file again, but you are unable to write to the file again until that happens, and it doesn't happen quickly.

Fortunately, in it's implementation of the IDisposable interface, in the Dispose() method, this.Close() is called. So, when you do this:
Expand|Select|Wrap|Line Numbers
  1. using (XmlTextWriter xt = new XmlTextWriter(somePath, null))
  2. {
  3.  
  4. }
when that code block exits, it automatically calls the .Dispose() method on the object in the using statement. Your stream will close, the process will release the lock on the file.

Sorry for the long explanation, but I feel that it is important to understand what is going on under the covers.
Jun 24 '09 #4
Chiv
2
Thanks everyone for your replies. Sorry I couldn't get back to you before, I was on a trip. I prefer long explanations, they're just...better. Anyway, I finally made it work, thanks again!
Jul 3 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
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:...
8
by: Gabe Moothart | last post by:
Hi, I'm writing a windows service which interacts with a separate process. Basically, it calls a process which creates a file, and then my service reads that file. The problem is, the external...
4
by: funkmusha | last post by:
I am trying to read a log file using vb.net. I get an error stating "The process cannot access the file 'C:\test.log' because it is being used by another process." Below is a sample of what I am...
5
by: bulldog8 | last post by:
I've read numerous posts and have tried multiple approaches that I found, but just cannot get a file renamed. I am using VB.Net 2002 ... Here is what I have tried: 1) Code common to all...
16
by: Eran.Yasso | last post by:
Hi, I have a mdb file shared in the LAN. I want to write app that verifies if it's open. If the file is not open, then my app can open the file. if the file is used, then the app won't open it....
8
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; I copied a new set of files over to my ASP.NET dir (and subdirs) on our test server. This replaced every file for the app. When I first then tried to bring it up, I got the below error. ...
0
nightangel
by: nightangel | last post by:
Hi dude,what i was done in my application is uploading a image file to my server using FTP, it work great when pushing a file into the server path using FTP. The problem i met now is i need to do a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.