directory copy

G

GS

from the online help, looks like to code step by step to copy content of
directory to another whereas I can use online line code for directory move
or delete.
I am using 2005 express .net 2
I did not any thing(or I did not have the right search term ) for copy
contents of directory to another and replacing /delete all files in the
target directory

Is there an easier way?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I do not remember any clas sin the framework that does it.
You have two options:
1- Write a recursive method that copy and recreate the struct (using
File.Copy & Directory.CreateDirectory)
2- use xcopy

I would recommend the second option.
 
A

Arne Vajhøj

GS said:
from the online help, looks like to code step by step to copy content of
directory to another whereas I can use online line code for directory move
or delete.
I am using 2005 express .net 2
I did not any thing(or I did not have the right search term ) for copy
contents of directory to another and replacing /delete all files in the
target directory

Is there an easier way?

For inspiration:

private void XCopy(string dir1, string dir2)
{
string[] files = Directory.GetFiles(dir1);
foreach (string f in files) {
File.Copy(f, dir2 + f.Substring(dir1.Length), true);
}
string[] dirs = Directory.GetDirectories(dir1);
foreach (string d in dirs) {
XCopy(d, dir2 + d.Substring(dir1.Length));
}
}

Arne
 
G

gs

Thank you. I got it working form your inspiration. I tumbled at the for
(string sfile in aDir)
the sfile actually contained the entire file spec including path.
that was not a big deal once found out what is the culprit. took care of by
using indexOfRev and substring

Arne Vajhøj said:
GS said:
from the online help, looks like to code step by step to copy content of
directory to another whereas I can use online line code for directory
move
or delete.
I am using 2005 express .net 2
I did not any thing(or I did not have the right search term ) for copy
contents of directory to another and replacing /delete all files in the
target directory

Is there an easier way?

For inspiration:

private void XCopy(string dir1, string dir2)
{
string[] files = Directory.GetFiles(dir1);
foreach (string f in files) {
File.Copy(f, dir2 + f.Substring(dir1.Length), true);
}
string[] dirs = Directory.GetDirectories(dir1);
foreach (string d in dirs) {
XCopy(d, dir2 + d.Substring(dir1.Length));
}
}

Arne
 
G

gs

Actually if I copied you code verbatim and modify from there I could have
avoided the trouble.

was not paying attention.

Arne Vajhøj said:
GS said:
from the online help, looks like to code step by step to copy content of
directory to another whereas I can use online line code for directory
move
or delete.
I am using 2005 express .net 2
I did not any thing(or I did not have the right search term ) for copy
contents of directory to another and replacing /delete all files in the
target directory

Is there an easier way?

For inspiration:

private void XCopy(string dir1, string dir2)
{
string[] files = Directory.GetFiles(dir1);
foreach (string f in files) {
File.Copy(f, dir2 + f.Substring(dir1.Length), true);
}
string[] dirs = Directory.GetDirectories(dir1);
foreach (string d in dirs) {
XCopy(d, dir2 + d.Substring(dir1.Length));
}
}

Arne
 

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