C#程序B/S打包下载

       protected void btnAdd_Click(object sender, EventArgs e)
        {
            // 创建临时文件夹
            string Folder = DateTime.Now.ToString("yyyymmddHHMMss");
            string tempFolder = Path.Combine(Server.MapPath("/file"), Folder);
            Directory.CreateDirectory(tempFolder);
            string[] fs = Directory.GetFiles(Server.MapPath("/files"));
            for (int i = 0; i < fs.Length; i++)
            {
                File.Copy(fs[i], tempFolder + fs[i].Substring(fs[i].LastIndexOf('\\'), fs[i].Length - fs[i].LastIndexOf('\\')));
            }

            //产生RAR文件,及文件输出
            RARsave(tempFolder, tempFolder, Folder);
            ResponseFile(tempFolder + @"\" + Folder + ".rar");
        }
        public void RARsave(string patch, string rarPatch, string rarName)
        {
            String the_rar;
            RegistryKey the_Reg;
            Object the_Obj;
            String the_Info;
            ProcessStartInfo the_StartInfo;
            Process the_Process;
            try
            {
                the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");
                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                the_rar = the_rar.Substring(1, the_rar.Length - 7);
                Directory.CreateDirectory(patch);

                //命令参数
                //the_Info = " a    " + rarName + "  " + @"C:Test?70821.txt"; //文件压缩
                the_Info = " a " + rarName + "  " + patch + "  -r";
                the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = "WinRar";//the_rar;
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                //打包文件存放目录
                the_StartInfo.WorkingDirectory = rarPatch;
                the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void ResponseFile(string fileName)
        {

            FileInfo fileInfo = new FileInfo(fileName);
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.ContentType = "application/octet-stream";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.WriteFile(fileInfo.FullName);
            Response.Flush();
            string tempPath = fileName.Substring(0, fileName.LastIndexOf("\\"));
            DelDir(tempPath);
            Directory.Delete(tempPath);
            Response.End();
         
          
        }
        private void DelDir(string path)
        {
            string[] fs = Directory.GetFiles(path);
            for (int i = 0; i < fs.Length; i++)
            {
                File.Delete(fs[i]);
            }
              
        }

posted @ 2011-11-02 14:40  .NET技术  阅读(482)  评论(0)    收藏  举报
网站:化妆品批发排行榜http://www.cosmetic-top.com/