Tuesday, June 17, 2008

DirectoryCopy method (.NET, C#)

There is no such method in FCL, so here is my implementation :)


        public static void DirectoryCopy(string from, string to)
{
if (String.IsNullOrEmpty(from) || String.IsNullOrEmpty(to))
return;
if (!Directory.Exists(from))
return;
if (!Directory.Exists(to))
{
try
{
Directory.CreateDirectory(to);
}
catch (Exception e)
{
//Log.LogException(Errors.CANNOT_COPY_FILE, e);
}
}
DirectoryInfo dir = new DirectoryInfo(from);
FileInfo[] files = dir.GetFiles();
if (files != null && files.Length > 0)
{
foreach (FileInfo file in files)
{
try
{
file.CopyTo(Path.Combine(to, file.Name));
}
catch (Exception e)
{
//Log.LogException(Errors.CANNOT_COPY_FILE, e);
}
}
}
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
DirectoryCopy(subDir.FullName, Path.Combine(to, subDir.Name));
}
}

1 comment:

akhzawa said...

Assalamualaikum sir...
I search in google bout Som kohonen and I'll find in your page..Then I'll try to my Visual studio..Thanks for the code..I'm from indonesia and my study at computer science,,I try to contact you in a mail but i dont find...Please add my google at akhzawa@gmail.com,,,I wanna share and ask you bout ANN artifivicial neural network...Key??Thanks..Nice to meet my muslim brother here..Nice to meet u..