By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

Copying Directory / Subdirectory and Files

P: n/a
Does anyone know the easiest way, in C#, to copy all directories and files
starting with some root directory? For example, how might I copy all files
and directories starting with 'C:\Program Files\' to some destination
directory.

Thanks.
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Look at the System.IO namespace.

Using the DirectoryInfo object, you can iterate through its files and
subdirectories and copy each file using the CopyTo method.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Amos Soma" <amos_j_soma@y_a_hoo.com> wrote in message
news:B4********************@buckeye-express.com...
Does anyone know the easiest way, in C#, to copy all directories and files
starting with some root directory? For example, how might I copy all files
and directories starting with 'C:\Program Files\' to some destination
directory.

Thanks.

Nov 16 '05 #2

P: n/a
Thanks Ben. I know I can do that. I was hoping this wasn't the 'easiest' way
to do it.

"Ben Lucas" <be*@nospam.solien.nospam.com> wrote in message
news:VL********************@comcast.com...
Look at the System.IO namespace.

Using the DirectoryInfo object, you can iterate through its files and
subdirectories and copy each file using the CopyTo method.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Amos Soma" <amos_j_soma@y_a_hoo.com> wrote in message
news:B4********************@buckeye-express.com...
Does anyone know the easiest way, in C#, to copy all directories and files starting with some root directory? For example, how might I copy all files and directories starting with 'C:\Program Files\' to some destination
directory.

Thanks.


Nov 16 '05 #3

P: n/a
Hi Amos

If you are willing to use interop the FileSystemObject offers a CopyFolder
method...

HTH

Nigel Armstrong

"Amos Soma" wrote:
Thanks Ben. I know I can do that. I was hoping this wasn't the 'easiest' way
to do it.

"Ben Lucas" <be*@nospam.solien.nospam.com> wrote in message
news:VL********************@comcast.com...
Look at the System.IO namespace.

Using the DirectoryInfo object, you can iterate through its files and
subdirectories and copy each file using the CopyTo method.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

"Amos Soma" <amos_j_soma@y_a_hoo.com> wrote in message
news:B4********************@buckeye-express.com...
Does anyone know the easiest way, in C#, to copy all directories and files starting with some root directory? For example, how might I copy all files and directories starting with 'C:\Program Files\' to some destination
directory.

Thanks.



Nov 16 '05 #4

P: n/a

"Amos Soma" <amos_j_soma@y_a_hoo.com> wrote in message
news:0s********************@buckeye-express.com...
Thanks Ben. I know I can do that. I was hoping this wasn't the 'easiest'
way
to do it.


There is an easier way, use System.Management classes and WMI's
Win32_Directory class .

Following is a sample:

using System.Management;
.....

string dirName = @"c:\\source"; // beware the double quotes!
string objPath = "Win32_Directory.Name=" + "\"" + dirName + "\"";

using (ManagementObject dir= new ManagementObject(objPath))
{
ManagementBaseObject inputArgs = dir.GetMethodParameters("CopyEx");
inputArgs["FileName"] = "C:\\destination";
inputArgs["Recursive"] = true; // Recurse folder and subfolders
ManagementBaseObject outParams = dir.InvokeMethod("CopyEx", inputArgs,
null);
uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
if(ret == 0)
Console.WriteLine("Success");
else Console.WriteLine("Failed with error code: {0}", ret);
}

Willy.
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.