带密码的RAR包的下载和解压

         今天,项目上要用到带密码的RAR压缩包的下载和解压。尽管网上有很多源码,但感觉还是花了很多时间,而且看的有点晕了。不过,终于还是搞明白了。简单整理下,以供参考。

        一,下载网络文件到本地

        public void DownloadFile()
        {
            //网络文件全路径
            string path = @"http://www.baidu.com/img/baidu_sylogo1.gif";
            //文件名
            string filename = path.Substring(path.LastIndexOf('/') + 1);
            //本地临时文件夹
            string fileDir = Environment.GetEnvironmentVariable("TEMP");
            //目标文件全名
            string fullname = fileDir + @"\" + filename;

            try
            {
                WebClient myWebClient = new WebClient();
                myWebClient.DownloadFile(path, fullname);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        二,解压缩带密码RAR

        /// <summary>
        /// WinRAR解压缩
        /// </summary>
        /// <param name="rarFile">RAR压缩包全路径</param>
        /// <param name="unRarDir">解压目录文件夹</param>
        public void unCompressRAR(string rarFile, string unRarDir)
        {
            //WinRAR软件路径
            string winrarPath;
            //WinRAR对应注册表
            RegistryKey winrarKey;
            //解压密码
            string password="123456";

            try
            {
                winrarKey=Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
                winrarPath=winrarKey.GetValue("").ToString();
                winrarKey.Close();

                ProcessStartInfo startInfo=new ProcessStartInfo ();
                startInfo.FileName=winrarPath;
                //-p是密码命令,-y是对话框结果全为是命令
                startInfo.Arguments="x -p"+password+" "+rarFile+" "+unRarDir+" -y";
                startInfo.WindowStyle=ProcessWindowStyle.Hidden;
                startInfo.WorkingDirectory=rarFile;

                Process uncompressProcess=new Process ();
                uncompressProcess.StartInfo=startInfo;
                uncompressProcess.Start();
                uncompressProcess.WaitForExit();
                uncompressProcess.Close();
                
            }
            catch(Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值