C#中拷贝整个文件夹下的所有文件到指定目录

<strong><span style="font-size:18px;">public class File
    {
        /// <summary>
        /// copy文件夹下的所有文件
        /// </summary>
        /// <param name="oPath">要拷贝的文件夹路径</param>
        /// <param name="nPath">目标路径</param>
        public static void MoveFile(string oPath, string nPath)
        {
            string flag = "下载成功";
            try
            {
                if(!Directory.Exists(nPath))
                {
                    Directory.CreateDirectory(nPath);
                }
                DirectoryInfo sDir = new DirectoryInfo(oPath);
                FileInfo[] fileArray = sDir.GetFiles();
                foreach(FileInfo file in fileArray)
                {
                    file.CopyTo(nPath + "\\" + file.Name , true);
                }

                DirectoryInfo dDir = new DirectoryInfo(nPath);
                DirectoryInfo[] subDirArray = sDir.GetDirectories();
                foreach(DirectoryInfo subDir in subDirArray)
                {
                    MoveFile(subDir.FullName , nPath + "\\" + subDir.Name);
                }
                isSuccess = true;
            }
            catch(Exception ex)
            {
                flag = ex.ToString();
            }
                   
        }

        /// <summary>
        /// 是否成功下载
        /// </summary>
        public static bool isSuccess = false;
  }</span></strong>
<strong><span style="font-size:18px;">配合调出路径选择的对话框:</span></strong>
<pre class="csharp" name="code"><strong><span style="font-size:18px;">                        FolderBrowserDialog fbd = new FolderBrowserDialog();//选择文件的对话框是openfiledialog
                        if (fbd.ShowDialog() == DialogResult.OK)
                        {
                            App app = listView1.SelectedItems[0].Tag as App;
                            int idx = app.AppAddress.LastIndexOf('\\');
                            if (idx >= 0)
                            {
                                string path = fbd.SelectedPath + "\\" + app.AppAddress.Substring(idx + 1);
</span></strong>
<strong><span style="font-size:18px;">                                //对话框选择路径的获取 fbd.SelectedPath</span></strong>
<strong><span style="font-size:18px;">                                File.MoveFile(app.AppAddress, path);</span></strong>
<strong><span style="font-size:18px;">//为了保证选择的路径下也有原文件夹,所以要在对话框内选择的路径后面加上原文                                                                      件夹的名字。这部分操作是:根据原来文件夹路径的最后一个"\",截取后面的字符串,再加到选择的目标路径后面。</span></strong>
<strong><span style="font-size:18px;">                       string a = "wdadada\\awdadw\\aweadawd\\awdawdaw\\aasdad";</span></strong>
<strong><span style="font-size:18px;">                       int idx = a.LastIndexOf('\\');</span></strong>
<strong><span style="font-size:18px;">                       if (idx >= 0) string path = a.SubString(idx + 1);</span></strong>
目标路径加上path
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值