473,387 Members | 1,530 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,387 software developers and data experts.

Copying Directory / Subdirectory and Files

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
4 11698
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Cotter | last post by:
First - is this the right venue for Visual C# questions? How do you duplicate a project. So if I have a directory c:\c#\projects\proj_1 Containing proj_1.sln, proj_1.suo and a subdirectory...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
9
by: Benny Ng | last post by:
Hi,all, How to let the sub-directory to avoid the authentication control from Root's webconfig? I heard that we can add a new web.config to the sub-directory. And then we can slove the problem....
3
by: Stimp | last post by:
I created a project which works fine on my local server, but when I uploaded it to the live server I got the following error when I tried to run it... "The path '/resources$' maps to a directory...
4
by: zMisc | last post by:
Is it possible to copy a table from one schema to another schema by just copying the frm file to the directory for the new schema? What is the best way to create a new database with all the...
2
by: Danny Boy | last post by:
Hi, "Snif" is an excellent little script to index files and directories. It's just one single file, and the advantage is that you merely drop it into the directory you want to index, point a...
13
by: lawpoop | last post by:
Hello all - I have a two part question. First of all, I have a website under /home/user/www/. The index.php and all the other website pages are under /home/user/www/. For functions that are...
4
by: supriyamk | last post by:
Hi, I am trying to search a directory for subdirectories, recreate the subdirectories elsewhere and copy only certain files from current subdirectory to new subdirectory. In other words i am...
1
by: Claire | last post by:
Ive written a small string resource building utility that I send out to our translators. I have a setup project for each language we support, which picks out a group of 12 english resx files plus...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.