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