473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Problem with File.Copy() - skipping files?

Dan
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;
}

Sep 17 '06 #1
1 2716
Dan
Nevermind... can't believe I missed that my forech loop was insde the try /
catch :)

"Dan" wrote:
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;
}
Sep 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Danieltbt05 | last post by:
just installed SQL server 2000 and using my client , i can't locate the server. I used SQL query analyzer to search but no servers were found. Error message is as below Server : Msg17,level...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
13
by: Bob | last post by:
I have been working on the following program. The goal is to have a tokenizing routine that avoids some of the problems of strtok(), the comments should explain the features. This runs fine on...
6
by: plmanikandan | last post by:
Hi, I need to split the value stored in a string and store them to another charrecter array.I am using strtok function.But i am getting invalid output when there is no value between delimiter ...
6
by: Richard | last post by:
Which way would you guys recommened to best parse a multiline file which contains two fields seperated by a tab. In this case its the linux/proc/filesystems file a sample of which I have included...
11
by: ZMY | last post by:
Dear all, I am a real newbie for both python and QNX, but I am still trying to compile Numeric-24.2 under QNX4.25 with python 2.2. I got following error message: $ sudo python setup.py...
10
by: Joah Senegal | last post by:
Hello all, I want something that should be realy really simple.... but I dont get it.. searched google and stuf, but found nothing useful. I;m opening a txt-file with a fstream. Now I want to...
1
by: wavespirit75 | last post by:
Hi, I wrote a program which will use ffmpeg to convert media files. Media files can be converted with this string array being passed in to Runtime.getRunTime().exec: String cmd = {ffmpeg path,...
0
by: Neal Becker | last post by:
Any ideas on this? bb-freeze test5-coded-pre.py WARNING: found xml.sax in multiple directories. Assuming it's a namespace package. (found in /usr/lib64/python2.5/site-packages/_xmlplus/sax,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.