上传压缩文件到服务器并解压

protected void Button1_Click(object sender, EventArgs e)
    {


        string file = FileUpload1.FileName;
        string filename = file.Substring(file.LastIndexOf("\\") + 1); 
        FileUpload1.SaveAs(Server.MapPath("File") + "\\" + filename);
        // string filepath = Server.MapPath("File");
        string filepath = "D:\\SSIS";       //解压后文件存放路径
        // unrar(Server.MapPath("File") + "\\" + filename, filepath);
        unrar(Server.MapPath("File") + "\\" + filename, filepath);
        File.Delete("D:\\Report\\File\\" + filename);//删除上传的压缩文件
         Exesql();//将txt文件导入数据库

 
    
    }


    public void unrar(string rarfilepath, string filepath)
    {


        string rar;
        RegistryKey reg;
        string args;
        ProcessStartInfo startInfo;
        Process process;
        reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");//WinRar位置
        rar = reg.GetValue("").ToString();
        reg.Close();
        rar = rar.Substring(1, rar.Length - 7);
        args = " X " + rarfilepath + " " + filepath;
        startInfo = new ProcessStartInfo();
        startInfo.FileName = rar;
        startInfo.Arguments = args;
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        process = new Process();
        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();//无限期等待进程 winrar.exe 退出 
        process.Close();
      
    }

posted on 2011-03-17 13:12  缘来  阅读(720)  评论(0)    收藏  举报

导航