复制文件及文件夹

static readonly private string path = AppDomain.CurrentDomain.BaseDirectory;

string file_name = "文件名.文件类型";
string s_path = path + file_name;
string d_path = folderBrowserDialog1.SelectedPath + "//" + file_name;
if (File.Exists(s_path))
{
          File.Copy(s_path, d_path, true);

          if (File.Exists(d_path))
               lb_error_info.Text = "复制文件" + file_name + "至" + folderBrowserDialog1.SelectedPath + "成功!/n";
}
else
{
          lb_error_info.Text = "源文件不存在!";
          return;
}

当然,WindowsForm里有FolderBrowserDialog和Label控件,分别名为folderBrowserDialog1和lb_error_info

 

//拷贝bin/Debug下文件到目标文件夹
string source_path_Permanence = path + "Permanence//";
string dest_path_Permanence = folderBrowserDialog1.SelectedPath + "//Permanence//";
CopyFolder(source_path_Permanence, dest_path_Permanence);

 

private void CopyFolder(string source_path, string dest_path)
{
        //检查文件夹
        if (!Directory.Exists(dest_path))
             Directory.CreateDirectory(dest_path);
        //子文件夹
        foreach (string sub_path in Directory.GetDirectories(source_path))
             CopyFolder(sub_path + "//", dest_path + Path.GetFileName(sub_path) + "//");
        //文件
        foreach (string file in Directory.GetFiles(source_path))
                File.Copy(file, dest_path + Path.GetFileName(file), true);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值