G
Guest
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.
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;
}