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

File.Copy error

Hello,
I am trying to overwrite a file in this way:
File.Copy(this.file_name,this.file_name,true);

The problem is that I get this error:
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file "C:\Documents
and Settings\My Documents\abc.xml" because it is being used by another
process.

I did the :
this.xml_reader.Close();
before I called the File.Copy but I still get this error,why?

Thank you!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
2 2043
What are you trying to accomplish? Your method call would, if it worked -
which it won't - just read this.file_name and re-write it to itself. If
you're going to overwrite the file, the assumption is that you want to
modify it in some manner first.

Dale Preston
MCAD, MCDBA, MCSE
"juli jul" <ju******@yahoo.com> wrote in message
news:ev**************@TK2MSFTNGP15.phx.gbl...
Hello,
I am trying to overwrite a file in this way:
File.Copy(this.file_name,this.file_name,true);

The problem is that I get this error:
An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file "C:\Documents
and Settings\My Documents\abc.xml" because it is being used by another
process.

I did the :
this.xml_reader.Close();
before I called the File.Copy but I still get this error,why?

Thank you!

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #2
As Dale pointed out, your call doesn't make sense and cannot work.

File.Copy will open the file provided as the first parameter in read-only
mode, open the file provided as the 2nd parameter in write mode. You get the
exception because if you open a file, you cannot open the same file in write
mode again.. write mode requires exclusive access.

So, if for whatever reason you need to overwrite the file with itself.. open
it, read its content to memory, close it again, then open it again in write
mode using the proper flags to indicate that if the file exists it should be
overwritten, then write to the file from the buffer.

Or somply do a File.Copy(file1, file1-temp); File.Delete(file1);
File.Move(file1-temp, file1).

Regards
Stephan
Nov 17 '05 #3

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

Similar topics

5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
2
by: RitaG | last post by:
Hi. In a VB.Net program I use File.Copy to copy some zipped (and text) files from one server to another. I'm required by management to do a file compare after the copy has completed. Is the...
8
by: Sarah | last post by:
I need to access some data on a server. I can access it directly using UNC (i.e. \\ComputerName\ShareName\Path\FileName) or using a mapped network drive resource (S:\Path\FileName). Here is my...
5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
11
by: sonajain | last post by:
Hi, The database transaction log file is full. I cannot delete the log file. when I try to take backup it throws error. Any other way so that I can write more transaction without any problem....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.