首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请问一上文件以及文件夹资料拷贝(备份)有关问题

2012-07-02 
请教一下文件以及文件夹资料拷贝(备份)问题本人菜鸟一枚,先在此谢过各位高手我现在想实现的是一个数据备份

请教一下文件以及文件夹资料拷贝(备份)问题
本人菜鸟一枚,先在此谢过各位高手
我现在想实现的是一个数据备份的功能,就是一个按钮点按之后,出现一个SaveFileDialog,然后选择保存的路径,点击确定之后自动将运行的相对路径下的b.mdf和c.ldf以及resource文件夹下的A文件夹拷贝(备份)到SaveFileDialog的路径下,请问该如何实现呢?我想看下实现的代码~
现在只有那么多:
  private void DataBackupButton_Click(object sender, EventArgs e)
  {
  SaveFileDialog sfd = new SaveFileDialog();
  sfd.Title = "选择文件备份路径";
  ofd.Filter = "All Files(*.*)|*.*";
  if (sfd.ShowDialog() == DialogResult.OK)
  { 
  }
  }

[解决办法]
你应该用FolderBrowserDialog 而不是 SaveFileDialog
[解决办法]
楼主是WindowsFormsApplication还是WebApplication?

1 如果是前者楼主可以加一个folderBrowserDialog控件,可以让你选择保存路径,string

 path=folderBrowserDialog1.SelectedPath.ToString();

2 如果是后者可以加一个FileUpload控件,也可以选择备份保存路径:string 

path=FileUpload1.PostedFile.FileName.ToString();
[解决办法]

C# code
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));        }    }} 

热点排行