日期:2014-05-18 浏览次数:21133 次
private void DataBackupButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "选择文件备份路径";
if (fbd.ShowDialog() == DialogResult.OK)
{
string[] files = { "b.mdf", "c.ldf", "resource\\A" };
string selectedPath = fbd.SelectedPath;
foreach (string f in files)
{
string sourcePath = Path.GetFullPath(f);
string destinationPath = Path.Combine(selectedPath, f);
if (File.Exists(sourcePath))
{
Copy(new FileInfo(sourcePath),destinationPath);
}
else if (Directory.Exists(sourcePath))
{
Copy(new DirectoryInfo(sourcePath), destinationPath);
}
}
}
}
private void Copy(FileSystemInfo fsi, string destPath)
{
if (fsi is FileInfo)
{
((FileInfo)fsi).CopyTo(destPath);
}
else if (fsi is DirectoryInfo)
{
Directory.CreateDirectory(destPath);
foreach (FileSystemInfo child in ((DirectoryInfo)fsi).GetFileSystemInfos())
{
Copy(child, Path.Combine(destPath, child.Name));
}
}
}