C# .NET - moving files from one folder to another

Asked By toink toink on 09-Mar-06 03:51 AM
i have these lines of code which transfers files from one folder to another. but an error occurs when the file to transfer is being used by another process... 


[CODE]
private int MoveFiles(String sourcePath, String destinationPath)
		{ 
			int fileCounter = 0; 

			if (destinationPath.LastIndexOf("\\") != destinationPath.Length - 1) 
			{ 
				destinationPath += "\\"; 
			} 

			foreach (string sFile in Directory.GetFiles(sourcePath)) 
			{ 
				string extension = sFile.Substring(sFile.LastIndexOf(".")); 

				if (extension == ".xml" || extension == ".XML") 
				{ 
					if (File.Exists(string.Concat(destinationPath, Path.GetFileName(sFile)))) 
					{ 
						if (MessageBox.Show(String.Format("{0}: already exists. Replace?", string.Concat(destinationPath, Path.GetFileName(sFile))), "TODO", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
						{ 
							File.Delete(string.Concat(destinationPath, Path.GetFileName(sFile))); 
							File.Move(sFile, string.Concat(destinationPath, Path.GetFileName(sFile))); 
							++fileCounter; 
						} 
					} 
					else 
					{ 
						File.Move(sFile, string.Concat(destinationPath, Path.GetFileName(sFile))); 
						++fileCounter; 
					} 
				} 
			} 
			return fileCounter; 
		} 

[/CODE]



the error will occur in this line
[CODE]

						File.Move(sFile, string.Concat(destinationPath, Path.GetFileName(sFile))); 
[/CODE]

how will i check if the file is being used by another process before i'll transfer it? thanks

moving files from one folder to another - Asked By Pankaj Sharma on 09-Mar-06 04:43 AM

You should use Try Catch block to know the problem exactly.

Some times problem occurs when the handle to file is not kept.

By the way insert this code 

[CODE]
using (FileStream fs = File.Create(path)) {}
[/CODE]
just after this line

[CODE]
if (File.Exists(string.Concat(destinationPath, Path.GetFileName(sFile))))      	
{
....
.....
...
[/CODE]
and check whether problem persists.

review - Asked By toink toink on 09-Mar-06 04:57 AM

the code is quite confusing...
which path am i going to place inside the parentheses? the destination path? or the sourcepath? what is the effect of using these lines?

review - Asked By Pankaj Sharma on 09-Mar-06 05:03 AM

place your source path inside the parentheses.

this statement just creates a file stream around the file and closes it explicitely.
Handle System IO IOException Exception - Asked By Amit Choudhary on 09-Mar-06 05:34 AM
You will have to make to it read ony before moving the file or 
Handle System.IO.IOException Exception in try catch block and massage to user accordingly .
You can't move file when its open in writable mode.
check - Asked By toink toink on 09-Mar-06 05:59 AM
i already tried the try catch method.. and it worked fine
but what i really like to do is to check if it is used by another process or not rather than a try catch block.. any other idea? still got a problem with it... thanks
The Only Way - Asked By F Cali on 09-Mar-06 10:15 AM
As far as I know, that's the only way for you to determine if the file is being used or not.  In your try/catch block, you can check for the error code or error message and if it says file is being used, then the file is being used.