maztt
maztt

Reputation: 12304

c# file move (file is already used by another process)

I am moving files from one folder to another. If I try to move the entire folder to the folder within that particular folder I cannot do that. But if I move it outside it works fine. How can I move my folder to the folder within that folder?

this gives error

  if (System.IO.Directory.Exists(PhysicalPath))
 {
      string sourcePath = "c:\\copypath\\";
      string targetPath = "c:\\copypath\\abc\\";
      System.IO.Directory.Move(sourcePath, targetPath);
 }

this works fine

 if (System.IO.Directory.Exists(PhysicalPath))
 {
      string sourcePath = "c:\\copypath\\";
      string targetPath = "c:\\abc\\";
      System.IO.Directory.Move(sourcePath, targetPath);
 }

Upvotes: 1

Views: 5927

Answers (4)

Twelve47
Twelve47

Reputation: 3982

Trying to "c:\copypath\" into "c:\copypath\abc\" won't work because it doesn't make sense.

If you move the copypath folder then it won't exist anymore, so how will the target folder (which is a subfolder) of it exist?

You could move all of the child files of "c:\copypath\" into "c:\copypath\abc\" instead, which wouldn't cause a problem (again assuming you don't try to copy abc into itself).

Upvotes: 5

Alex KeySmith
Alex KeySmith

Reputation: 17111

Perhaps you need to move all the files inside the folder, rather than the folder itself?

Upvotes: 1

Novice
Novice

Reputation: 2487

I guess the sourcepath will be in use (copying of files) and since the destination path is a subfolder of source path it might give you "Currently used by another process error".

Upvotes: 0

Naveed Butt
Naveed Butt

Reputation: 2901

It throws away the error, because you are trying to move a directory in its own subdirectory. This would give an error in windows as well...

You can do this as an alternate, if you are trying to copy rather than move...

if (System.IO.Directory.Exists(PhysicalPath))
{
      string sourcePath = "c:\\copypath\\";
      string targetPath = "c:\\copypath\\abc\\";
      System.IO.Directory.Copy(sourcePath, targetPath);
}

Upvotes: 2

Related Questions