Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   no Copy in C#??? (http://www.programmingforums.org/showthread.php?t=15837)

BstrucT May 16th, 2008 6:09 AM

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

kruptof May 16th, 2008 7:36 AM

Re: no Copy in C#???
 
This link has a nice list of file functions

BstrucT May 16th, 2008 8:26 AM

Re: no Copy in C#???
 
Ooh, I see there is one there.

Thanks kruptof!

>BstrucT


All times are GMT -5. The time now is 4:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC