C#将一个文件所在文件夹里面的所有文件复制到另一个文件夹。复制当个文件到另一个文件夹

   using Ionic.Zip;
   using System.Text;
   using System;
   using System.Collections.Generic;

   protected void Button1_Click(object sender, EventArgs e)
    {
        string _sTaskId = TaskID;
        string _sFormId = FormID;
        Unionstars.Bpm.Task item = Unionstars.Bpm.Task.FindByKey(_sTaskId);
        if (item != null)
        {
            try
            {
                //通过taskid获取Tokenid
                IList<Unionstars.Bpm.TokenLast> tokenlastlist = Unionstars.Bpm.TokenLast.FindAll(new string[] { "TaskId" }, new string[] { item.ID });
                if (tokenlastlist != null && tokenlastlist.Count > 0)
                {
                    Unionstars.Bpm.TokenLast tokenlastitem = tokenlastlist[0];
                    //文件名
                    string dirName =  "游学" + (DateTime.Now.ToString("yyyyMMddHHmmss"));
                    //文件路径
                    string filePath = @AppDomain.CurrentDomain.BaseDirectory + @"UploadFile\游学文件下载\" + dirName + @"\";
                    string normalizedPath = System.Text.RegularExpressions.Regex.Replace(filePath, @"[/]{2,}", "/");
                    string[] aryReg = {"?", "<", ">", "(", ")", "*", "(", ")", "、", "\r\n"  };
                    for (int j = 0; j < aryReg.Length; j++)
                    {
                        filePath = filePath.Replace(aryReg[j], string.Empty);
                        //pdfPath = pdfPath.Replace(aryReg[j], string.Empty);
                    }
                    //压缩路径
                    string zippdfPath = AppDomain.CurrentDomain.BaseDirectory + @"UploadFile\游学文件下载\" + dirName + ".zip";
                    //文件夹不存在创建
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }

                    var list = Attachment.FindByName("FormID", FormID);
                    if (!string.IsNullOrEmpty(list.FilePath))
                    {
                        
                        string directoryPath = Directory.GetParent(list.FilePath).FullName;//获取文件所在饿文件夹地址
                        CopyFolder(directoryPath, filePath);
                    }
                    using (ZipFile zip = new ZipFile(zippdfPath, Encoding.UTF8))
                    {
                        zip.AddDirectory(filePath);
                        zip.Save();
                    }

                    Response.Clear();
                    Response.ClearContent();
                    Response.ClearHeaders();
                    //Response.AddHeader("Content-Disposition", "attachment;filename=" + BiaoTi.Text + (DateTime.Now.ToString("yyyyMMddHHmmss")) + ".zip");
                    //Response.AddHeader("Content-Transfer-Encoding", "binary");

                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("游学" + (DateTime.Now.ToString("yyyyMMddHHmmss")) + ".zip"));

                    Response.AddHeader("Content-Transfer-Encoding", "binary");
                    Response.ContentType = "application/octet-stream";
                    Response.ContentEncoding = System.Text.Encoding.Default;
                    Response.WriteFile(zippdfPath);
                    Response.Flush();
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                Unionstars.Trace.Log.WriteLine("拷贝文件失败1:" + ex.ToString());
            }
        }
        else
        {
            //提示失败
            string strScript = "<script>alert('未找到表单信息!');</script>";
            string key = "start";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), key, strScript);
        }
    }
    
    public static void CopyFolder(string sourceFolder, string destFolder)
    {
        try
        {
            //如果目标路径不存在,则创建目标路径
            if (!System.IO.Directory.Exists(destFolder))
            {
                System.IO.Directory.CreateDirectory(destFolder);
            }
            //得到原文件根目录下的所有文件
            string[] files = System.IO.Directory.GetFiles(sourceFolder);
            foreach (string file in files)
            {
                string name = System.IO.Path.GetFileName(file);
                string dest = System.IO.Path.Combine(destFolder, name);
                System.IO.File.Copy(file, dest, true);//复制文件
            }
            //得到原文件根目录下的所有文件夹
            //string[] folders = System.IO.Directory.GetDirectories(sourceFolder);
            //foreach (string folder in folders)
            //{
            //    string name = System.IO.Path.GetFileName(folder);
            //    string dest = System.IO.Path.Combine(destFolder, name);
            //    CopyFolder(folder, dest);//构建目标路径,递归复制文件
            //}
        }
        catch (Exception ex)
        {

            Unionstars.Trace.Log.WriteLine("拷贝文件失败2:" + ex.ToString());
        }

    }


//复制单个文件到另一个文件夹
 public static void CopyFile(string sourceFilePath, string destinationFolderPath)
    {
        // 获取源文件的完整路径和目标文件夹路径
        string destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(sourceFilePath));

        // 复制文件
        File.Copy(sourceFilePath, destinationFilePath, true); // true表示如果目标文件存在则覆盖它
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值