日期:2014-05-18  浏览次数:20855 次

关于C#调用系统中的WinRAR软件,来解压带密码压缩文件的问题
由于刚开始用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() == "")
  {<