I have made some improvements without making a change to the methodology.
Note: Yes, the performance difference between
string.format
and
concat
is negligable when you are working with File-IO. Still we are here to educate.
public static class FolderManager
{
public static Exception LastError { get; private set; }
public static void ClearLastError()
{
LastError = null;
}
public static bool CopyFolderContents(string sourcePath, string destinationPath)
{
string slashEndingSourcePath = sourcePath.EndsWith(@"\") ? sourcePath : sourcePath + @"\";
string slashEndingDestinationPath = destinationPath.EndsWith(@"\") ? destinationPath : destinationPath + @"\";
try
{
if (Directory.Exists(slashEndingSourcePath))
{
if (!Directory.Exists(slashEndingDestinationPath))
{
Directory.CreateDirectory(slashEndingDestinationPath);
}
foreach (string files in Directory.GetFiles(slashEndingSourcePath))
{
FileInfo fileInfo = new FileInfo(files);
fileInfo.CopyTo(slashEndingDestinationPath + fileInfo.Name, true);
}
foreach (string drs in Directory.GetDirectories(slashEndingSourcePath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(drs);
if (!CopyFolderContents(drs, slashEndingDestinationPath + directoryInfo.Name))
{
return false;
}
}
}
return true;
}
catch (Exception ex)
{
LastError = ex;
return false;
}
}
public static void StringTest()
{
long start = DateTime.Now.Ticks;
for (int i = 0; i < 10000000; i++)
{
string x = string.Format(@"{0}\{1}", "sdfsghhwerhresdgwe4tfg423tfgoiosdufgosdgwe4tfg423tfgoiosdufgo", "sdgwe4tfg423tfgoiosdufgo");
}
MessageBox.Show(new TimeSpan(DateTime.Now.Ticks - start).ToString());
start = DateTime.Now.Ticks;
for (int i = 0; i < 10000000; i++)
{
string x = "sdfsghhwerhresdgwe4tfg423tfgoiosdufgosdgwe4tfg423tfgoiosdufgo" + "\\" + "sdgwe4tfg423tfgoiosdufgo";
}
MessageBox.Show(new TimeSpan(DateTime.Now.Ticks - start).ToString());
}
}
Doing that 'computer thing' ever since the C64.
Sometimes I feel that being a programmer is much like being a doctor: You just have to know everything and if you don't, something dies.
Either being an application or a patient.
Oddly enough, more people care about the death of their application, than the massacre of people...