XCopy in .NET

B

Brian Link

Is there a Platform way to grab an entire directory and copy it? Or
will I have to iterate through the files and binary write them?

Thanks

BLink
Brian Link in St. Paul
 
F

Felix Wang

If you prefer to use P/Invoke, it would be best that you change the title of
the post to "XCopy in P/Invoke".

To achieve XCopy in .Net, we may need to enumerate the contents of the
directory by using Directory.GetFiles() and Directory.GetDirectories(). Then
use FileCopy() to copy the files one by one.

Alternatively, we can use the following:

using System.Diagnostics;

ProcessStartInfo p = new ProcessStartInfo("xcopy");
p.Arguments = "/s c:\source c:\dest";
p.CreateNoWindow = true;
Process.Start(p);

I hope this helps.
 
T

Tian Min Huang

Hello Brian,

Thanks for your post. As I understand, you want to copy a directory
directly without enumerating and copying each file and sub-folder. Please
correct me if there is any misunderstanding.

1. We are able to use the SHFileOperation() API to xcopy a directory.
Please refer to the following articles and sample:

SHFileOperation Function
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/pla
tform/shell/reference/functions/shfileoperation.asp

http://www.visualbasicforum.com/t125205.html

2. Since SHFileOperation() is unmanaged API, you may need to use P/Invoke
to call it from within your .NET app:

Consuming Unmanaged DLL Functions
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

3. I'd like to provide the following sample in case you will need to copy a
directory by iterating through the files in .NET:

Function to copy a directory to another place
http://www.codeproject.com/csharp/copydirectoriesrecursive.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top