473,396 Members | 2,158 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,396 software developers and data experts.

Move a file from one directory to other.

Hello!
I want to move file from one directory to other.
The exception is:
“The process cannot access the file because it is being used by another
process.”
Maybe it is because I use this file in the project, and do some operations
on it,
And then try to move it to other folder?
Maybe I forget something? ("Free" the file?) or something else ?
Thank U for your help ...

Value of the program:
bckUpFilePath = @"c:\BckUpRet\"
OrgFileName="DocDraft1.txt"

Expand|Select|Wrap|Line Numbers
  1. fi = di.GetFiles();
  2. bckUpFilePath = sBckUpDirectory + "\\";
  3. foreach (FileInfo fTemp in fi)
  4. {
  5. OrgFileName  = fTemp.Name;
  6. // Ensure that the target file does not exist.
  7. if (File.Exists(bckUpFilePath+OrgFileName))     <--- works
  8. File.Delete(bckUpFilePath+OrgFileName);           <--- works   // Move this
  9. file to another file.   fTemp.MoveTo(bckUpFilePath+OrgFileName);  <--- don’t
  10. work
  11. }
  12.  
Thanks again.
Jun 5 '06 #1
5 7302
It sure looks to me like you're deleting a file, and then trying to move
it...

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck
"Niron kag" <Ni******@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
Hello!
I want to move file from one directory to other.
The exception is:
"The process cannot access the file because it is being used by another
process."
Maybe it is because I use this file in the project, and do some operations
on it,
And then try to move it to other folder?
Maybe I forget something? ("Free" the file?) or something else ?
Thank U for your help ...

Value of the program:
bckUpFilePath = @"c:\BckUpRet\"
OrgFileName="DocDraft1.txt"

Expand|Select|Wrap|Line Numbers
  1.  fi = di.GetFiles();
  2.  bckUpFilePath = sBckUpDirectory + "\\";
  3.  foreach (FileInfo fTemp in fi)
  4.  {
  5.  OrgFileName  = fTemp.Name;
  6.  // Ensure that the target file does not exist.
  7.  if (File.Exists(bckUpFilePath+OrgFileName))     <--- works
  8.  File.Delete(bckUpFilePath+OrgFileName);           <--- works   // Move
  9.  this
  10.  file to another file.   fTemp.MoveTo(bckUpFilePath+OrgFileName);  <--- don't
  11.  work
  12.  }
  13.  

Thanks again.

Jun 5 '06 #2
Not to me it doesn't - unless "di" is the same as "bckUpFilePath"... rather,
it looks like he's deleting the /old/ backup (if it exists), then moving the
current fTemp to the same location.

My guess would be a FileStream (or similar) that hasn't been Close()d and
Dispose()d.

Some other thoughts... "copy" is more common with backups, no? And you might
want to think about what happens to the backup when the move/copy fails...
i.e. you just canned it "whoops, no backup".

Marc
Jun 5 '06 #3


"Marc Gravell" wrote:
Not to me it doesn't - unless "di" is the same as "bckUpFilePath"... rather,
it looks like he's deleting the /old/ backup (if it exists), then moving the
current fTemp to the same location.

My guess would be a FileStream (or similar) that hasn't been Close()d and
Dispose()d.

Some other thoughts... "copy" is more common with backups, no? And you might
want to think about what happens to the backup when the move/copy fails...
i.e. you just canned it "whoops, no backup".

Marc

Marc Gravell,
Thank you I just closed the file and it works.
You really helped me!
Jun 5 '06 #4
For reference - to avoid this type of thing in future, "using" is your
friend...

using(FileStream stream = File.Open(blah)) {
// do something 'orribly complex
stream.Close();
}

This way, even if the method barfs with an exception the stream will get
Dispose()d and the file should be released. A little trickier if you want to
hold the stream for an indefinite time, but do-able.

Marc
Jun 5 '06 #5
Yes, you're right Marc. I've been too busy lately!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck
"Marc Gravell" <ma**********@gmail.com> wrote in message
news:Oy**************@TK2MSFTNGP02.phx.gbl...
Not to me it doesn't - unless "di" is the same as "bckUpFilePath"...
rather, it looks like he's deleting the /old/ backup (if it exists), then
moving the current fTemp to the same location.

My guess would be a FileStream (or similar) that hasn't been Close()d and
Dispose()d.

Some other thoughts... "copy" is more common with backups, no? And you
might want to think about what happens to the backup when the move/copy
fails... i.e. you just canned it "whoops, no backup".

Marc

Jun 5 '06 #6

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

Similar topics

1
by: bmgz | last post by:
I am have made a simple script that moves all desktop clutter (ie files that are not *.lnk) to a specified folder eg. c:\myhome\mydocs\desktopdebris\2003-12-16 ...
3
by: andrew | last post by:
I do not understand how this function should work. It seems to be different than when I drag a folder ... error message: Cannot create a file when that file already exists. c:\atest has...
1
by: rv | last post by:
I am developing an ASP.NET website with C#, VS 2003 and Framework 1.1. For some reason every time I execute a Directory.Move command, the next command takes 10-20 seconds to execute. Has anyone...
8
by: Ron Vecchi | last post by:
I know this has been asked before but I out of all the posts I've read regarding using Directory.Move and then the application restarting itself I have found no answer except that Sessions are...
2
by: Eje | last post by:
I have a webapplication on a server. One function is to build txt-files. These files should then be moved to a fixed directory on another computer in the company network. I have tried to use...
1
by: Matthew Eno | last post by:
I have a problem where I'm developing some code that moves files from one place to another (on the same drive) with the destination file having a new name. What's happening is that the line of...
3
by: Karel | last post by:
Hello, I have a VB.NET application where I want to move directories over a network. I tried this with system.io.directory.move, but that doesn't work over different volumes. Has anyone a...
3
by: Arpan | last post by:
A Form has a FileUpload, 2 Buttons & a TextBox web server controls. Using the FileUpload control, I want to give users the provision to move & delete files that DO NOT exist in C:\Inetpub\wwwroot...
0
by: Matt MacDonald | last post by:
Hi all, I'm having a really frustrating problem. I have a web application that needs to modify the directory structure of the underlying files. The problem I'm having is that after the system...
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
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
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...
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.