View Single Post
Old May 16th, 2008, 5:09 AM   #1
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 169
Rep Power: 1 BstrucT is on a distinguished road
no Copy in C#???

Hi Guys

Is there maybe a copy function for the C# language?
I can only find Directory.Move, Directory.Delete, etc. but no Copy function?

I know I can do it like this...

public static class CopyUtility
{
public static void CopyAllFiles
(string sourceDirectory, string targetDirectory, bool recursive)
{
                if (sourceDirectory == null)
                    throw new ArgumentNullException(@"C:/Synaps");
                if (targetDirectory == null)
                    throw new ArgumentNullException(@"C:/Backup");

CopyAllFiles(new DirectoryInfo(sourceDirectory), new
DirectoryInfo(targetDirectory), recursive);
}

public static void CopyAllFiles
(DirectoryInfo source, DirectoryInfo target, bool recursive)
            {
                if (source == null)
                    throw new ArgumentNullException(@"C:/Synaps");
                if (target == null)
                    throw new ArgumentNullException(@"C:/Backup");

                if (!source.Exists)
                    target.Create();

foreach (FileInfo file in source.GetFiles())
                {
                    file.CopyTo(Path.Combine(target.FullName, file.Name), true);
                }
                if (!recursive) return;
foreach (DirectoryInfo directory in source.GetDirectories())
{
CopyAllFiles(directory, new DirectoryInfo(Path.Combine
(target.FullName, directory.Name)), recursive);
}
}
}
Then just call the below at the let's say, button click event.
private void backupToolStripMenuItem_Click(object sender, EventArgs e)
{
CopyUtility.CopyAllFiles(@"C:/Synaps", @"C:/Backup", true);
}


But isn't there another way to do it though?

>BstrucT
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is offline   Reply With Quote