using System.IO;
using System.Diagnostics;
string today = DateTime.Today.Year.ToString().Trim() + DateTime.Today.Month.ToString().PadLeft(2, '0').Trim() + DateTime.Today.Day.ToString().PadLeft(2, '0').Trim();
string sourcePath = @"來源路徑\";
string targetPath = @"目的路徑\";
for (int i = 1; i < 1000; i++)
{
if (Directory.Exists(targetPath + "INVMB_REP" + today + i.ToString().PadLeft(3, '0')))
{
if (Directory.Exists(targetPath + "INVMB_REP" + today + (i + 1).ToString().PadLeft(3, '0')) == false)
{
Directory.CreateDirectory(targetPath + "INVMB_REP" + today + (i + 1).ToString().PadLeft(3, '0'));
targetPath = targetPath + "INVMB_REP" + today + (i + 1).ToString().PadLeft(3, '0');
break;
}
}
else
{
Directory.CreateDirectory(targetPath + "INVMB_REP" + today + "001");
targetPath = targetPath + "INVMB_REP" + today + "001";
break;
}
}
CopyDirectory(sourcePath, targetPath);
MessageBox.Show("檔案已備份完成","ahhsu的程式備份");
//開啟資料夾
ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe");
startInfo.Arguments = targetPath;
Process.Start(startInfo);
public static void CopyDirectory(string srcFolder, string dstFolder)
{
if (Directory.Exists(srcFolder) == true)
{
if (Directory.Exists(dstFolder) == false)
Directory.CreateDirectory(dstFolder);
DirectoryInfo srcDirectory = new DirectoryInfo(srcFolder);
foreach (FileInfo fi in srcDirectory.GetFiles())
{
try { System.IO.File.Copy(fi.FullName, dstFolder + Path.DirectorySeparatorChar + fi.Name); }
catch { }
}
foreach (DirectoryInfo di in srcDirectory.GetDirectories())
{
try { CopyDirectory(di.FullName, dstFolder + Path.DirectorySeparatorChar + di.Name); }
catch { }
}
}
}