由于刚开始用C#写东西,最近公司要做一个小工具,就是解压公司带固定解压密码的压缩文件,后缀为.rar,或.zip,我想调用系统本身带的WinRAR解压缩工具来执行解压,现在其他东西代码都写好了,结果解压的时候需要的解压密码加不进去,我把代码贴出来,请有经验的大大们帮我看看吧,添加解压密码的代码应该怎么写呢?非常感谢,大半夜的,呵呵,大家辛苦!
源码如下:
解压类:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.Win32;
using System.Security;
using System.Windows.Forms;
namespace VstTxt
{
    class Class2
    {
        ///检测是否安装了Winrar         public bool Exists()
        {            RegistryKey the_Reg =
                Registry.LocalMachine.OpenSubKey(
                    @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");            return !string.IsNullOrEmpty(the_Reg.GetValue("").ToString());        }        
        public string unCompressRAR(string unRarPatch, string rarPatch, string rarName)        {
            string the_rar;
            RegistryKey the_Reg;
            object the_Obj;
            string the_Info;
            try
            {
                the_Reg = 
                    Registry.LocalMachine.OpenSubKey(
                         @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");                the_Obj = the_Reg.GetValue("");
                the_rar = the_Obj.ToString();
                the_Reg.Close();
                //the_rar = the_rar.Substring(1, the_rar.Length - 7);
                if (Directory.Exists(unRarPatch) == false)                {
                    Directory.CreateDirectory(unRarPatch);
                } 50行:         the_Info = "x " + rarName + " " + unRarPatch + " -y";
                ProcessStartInfo the_StartInfo = new ProcessStartInfo();
                the_StartInfo.FileName = the_rar;
 53行:          //the_StartInfo.Password = StringToSecureString("C1_a2_P3_i4_N5_f6_o7");
                the_StartInfo.Arguments = the_Info;
                the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                the_StartInfo.WorkingDirectory = rarPatch;//获取压缩包路径                Process the_Process = new Process();
                the_Process.StartInfo = the_StartInfo;
                
                the_Process.StartInfo.UseShellExecute = false;
                the_Process.Start();
                the_Process.WaitForExit();
                the_Process.Close();            }
            catch (Exception ex)
            {
                throw ex;
            }
            return unRarPatch;
        }        static SecureString StringToSecureString(String value)
        {
            SecureString password = new SecureString();
            char[] pass = value.ToCharArray();
            for (int i = 0; i < pass.Length; i++)
            {
                password.AppendChar(pass[i]);
            }
            return password;
        }  
    }
}页面调用代码:
 private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.ToString().Trim() == "" || textBox2.Text.ToString().Trim() == "")
            {
                MessageBox.Show("‘解压文件地址’和‘目标文件地址’都不能为空!");
                return;
            }
            string pah1 = textBox1.Text.ToString().Trim();//-需要解压文件路径...
            string pah2 = textBox2.Text.ToString().Trim();//-文件解压目标路径...
            Class2 winrar = new Class2();
            try
            {
                if (winrar.Exists())
                {
                   string pah3= winrar.unCompressRAR(pah2.ToString().Trim(), "", pah1.ToString().Trim());
                   if (pah3.ToString() == pah2.ToString())
                   {
                       MessageBox.Show("解压成功!解压后的文件路径:" + pah3.ToString());
                       //this.Close();
                   }
                }
                else
                {
                    MessageBox.Show("系统检测到您计算机没安装‘WinRAR’解压软件,请先安装!");
                    return;
                }
            }
            catch (Exception ex)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                throw ex;
            }现在代码已经可以解压没有密码的压缩文件了,如果有密码的就会卡在输入密码那个界面就不往下执行了,需要手动输入密码,确认才会解压出来,请问大家:在那个加压类里面应该加上一句什么样的代码,才可以把密码自动带进去呢?开始的时候我在53行那里加了这样一句代码:the_StartInfo.Password = StringToSecureString("C1_a2_P3_i4_N5_f6_o7");括号内的就是固定的解压密码,可是运行发现没有用,一解压还是卡到输入密码那界面去了,清有经验的大大们帮帮忙吧....
开始的时候我在50行那里这样写过: the_Info = "x -pC1_a2_P3_i4_N5_f6_o7" + rarName + " " + unRarPatch + " -y";
可是还是不行...我不知道应该怎么处理了...大家帮帮忙吧..大半夜的..打扰大家..实在抱歉哈...

解决方案 »

  1.   

    http://blog.csdn.net/haitaoq/article/details/5303736
      

  2.   

    运行cmd, 再运行unrar.exe,然后它会列出所有命令行参数的简单说明。
    C:\Documents and Settings\wang>"C:\Program Files\WinRAR\UnRAR.exe"UNRAR 3.80 免费软件      版权 (C) 1993-2008 Alexander Roshal所有用法:     unrar <命令> -<开关 1> -<开关 N> <压缩文件> <文件...>
                   <@列表文件...> <解压路径\><命令>
      e             解压压缩文件到当前目录
      l[t,b]        列出压缩文件[技术信息,简洁]
      p             打印文件到标准输出设备
      t             测试压缩文件
      v[t,b]        详细列出压缩文件[技术信息,简洁]
      x             用绝对路径解压文件<开关>
      -             停止扫描
      ac            压缩或解压后清除存档属性
      ad            添加压缩文件名到目标路径
      ap<格式>      添加路径到压缩文件中
      av-           禁用用户身份校验
      c-            禁用注释显示
      cfg-          禁用读取配置
      cl            名称转换为小写
      cu            名称转换为大写
      dh            打开共享文件
      ep            从名称中排除路径
      ep3           扩展路径为包含盘符的完全路径
      f             刷新文件
      id[c,d,p,q]   禁用消息
      ierr          发送所有消息到标准错误设备
      inul          禁用所有消息
      ioff          完成一个操作后关闭 PC 电源
      kb            保留损坏的已解压文件
      n<文件>       仅包含指定的文件
      n@            从标准输入设备读取文件名到包括
      n@<列表>      在指定的文件列表包括文件
      o[+|-]        设置覆盖模式
      oc            设置 NTFS 压缩属性
      or            自动重命名文件
      ow            保存或恢复文件所有者和组
      p[密码]       设置密码
      p-            不询问密码
      r             递归子目录
      ri<P>[:<S>]   设置优先级(0-默认,1-最小..15-最大)和以毫秒为单位的休眠时间
      sl<大小>      处理小于指定大小的文件
      sm<大小>      处理超过指定大小的文件
      ta<日期>      添加日期 <日期> 后修改的文件,日期格式 YYYYMMDDHHMMSS
      tb<日期>      添加日期 <日期> 前修改的文件,日期格式 YYYYMMDDHHMMSS
      tn<时间>      添加 <时间> 以后的文件
      to<时间>      添加 <时间> 以前的文件
      ts<m,c,a>[N]  保存或恢复文件时间(修改,创建,访问)
      u             更新文件
      v             列出所有卷
      ver[n]        文件版本控制
      vp            每卷之前暂停
      x<文件>       排除指定的文件
      x@            从标准输入设备读取要排除的文件名
      x@<列表>      排除指定列表文件中的文件
      y             假设对全部询问都回答是
      

  3.   

    本帖最后由 net_lover 于 2012-08-12 10:39:10 编辑
      

  4.   

    5楼您好!
    我解压密码那一句代码也是这样写的:
    string password = "C1_a2_P3_i4_N5_f6_o7";
    the_Info = "x -p" + password +rarName + " " + unRarPatch + " -y";
    可是到执行解压那一句:
    the_Process.Start();
    直接就过去了,根本执行不解压,
    要是不带密码的是可以解压出来的,像这样:
     the_Info = "x " + rarName + " " + unRarPatch + " -y";
    这是为什么呢...?
      

  5.   

    问题终于解决啦,非常感谢大家,我还是说一下解决的方法吧,其实很简单,大家不要笑话我就好-_-,
    方法就是在这句代码里面加一个空格:
    由原来的:
    string password = "C1_a2_P3_i4_N5_f6_o7";
    变成:
    string password = "C1_a2_P3_i4_N5_f6_o7 ";
    就可以了..因为加压的密码跟后面的路径不能紧挨着...解决办法就这么简单...可还是被卡在这里两天..5555