Problem with File.Copy() - Skipping files?

I have an application that I want to use for copying files. My goal is to copy a files, if a file is in use or not accessible because of security reasons I want to make note of that file then continue with the file copy process.

For some reason when my app is copying the C:\Windows\System32\Config folder it will fail on the first non-accessible file then ends the copy process. It only seems to happen on this folder (folder contains registry and eventvwr files, thus they are in use).

Can someone give me some help here, I am at a lost.

This chuck of code runs in a BackgroundWorker thread.

[code]

private void FileCopy(string srcdir, string destdir, bool recursive, ref DoWorkEventArgs e)

{

DirectoryInfo dir;

FileInfo[] files;

DirectoryInfo[] dirs;

string tmppath="";

//determine if the destination directory exists, if not create it

if (!Directory.Exists(destdir))

{

Directory.CreateDirectory(destdir);

}

dir = new DirectoryInfo(srcdir);

//if the source dir doesn't exist, throw

if (!dir.Exists)

{

throw new ArgumentException("source dir doesn't exist -> " + srcdir);

}

//get all files in the current dir

try

{

files = dir.GetFiles();

//loop through each file

foreach (FileInfo file in files)

{

//create the path to where this file should be in destdir

if (ProgressThread.CancellationPending)

{

e.Cancel = true;

return;

}

tmppath = Path.Combine(destdir, file.Name);

//copy file to dest dir

file.CopyTo(tmppath, true);

lblCurrentFile.Text = "Processing: " + file.FullName;

lblCurrentFile.Update();

_totalFilesCopied++;

}

//cleanup

files = null;

}

catch (Exception)

{

FailedFiles.Add(tmppath);

_totalFailedCopied++;

}

//if not recursive, all work is done

if (!recursive)

{

return;

}

try

{

//otherwise, get dirs

dirs = dir.GetDirectories();

//loop through each sub directory in the current dir

foreach (DirectoryInfo subdir in dirs)

{

//create the path to the directory in destdir

tmppath = Path.Combine(destdir, subdir.Name);

//recursively call this function over and over again

//with each new dir.

FileCopy(subdir.FullName, tmppath, recursive, ref e);

}

}

catch (UnauthorizedAccessException)

{

FailedFiles.Add(tmppath);

_totalFailedCopied++;

}

//cleanup

dirs = null;

dir = null;

}



Answer this question

Problem with File.Copy() - Skipping files?

  • Max Diamond

    absolutely correct, I just seen the mistake in the code. Serves me right I should always go over and over code from samples on the Internet :(.

    Thanks for the help


  • CharlieRussell

    Hi

    I'm not sure is this answer to your question but I find one logical mistake on your code.

    Move try catch block inside foreach loop. Now it break loop after first copy error.

    Yours

    Markku


  • Edmund

    ive actually posted some code here on these forums about copying files from one place to another - do some search under my username and see what it comes up with as i dont have the code handy with me. if you find it, hope it helps

  • Problem with File.Copy() - Skipping files?