您的位置:首页 > 编程语言 > C#

C#调用外部程序实现压缩和解压缩[原]

2008-11-11 08:58 661 查看
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using Microsoft.Win32;

using System.Diagnostics;

namespace WebDisk

{

/// <summary>

/// 压缩文件夹

/// </summary>

public class RarClass

{

public RarClass()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

#region 压缩文件

/// <summary>

/// 压缩文件

/// </summary>

/// <param name="DFilePath">需要压缩的文件夹或者单个文件</param>

/// <param name="DRARName">生成压缩文件的文件名</param>

/// <param name="DRARPath">生成压缩文件保存路径</param>

/// <returns></returns>

public static bool RAR(string DFilePath, string DRARName, string DRARPath)

{

String the_rar;

RegistryKey the_Reg;

Object the_Obj;

String the_Info;

ProcessStartInfo the_StartInfo;

Process the_Process;

try

{

the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRAR.exe/Shell/Open/Command");

the_Obj = the_Reg.GetValue("");

the_rar = the_Obj.ToString();

the_Reg.Close();

the_rar = the_rar.Substring(1, the_rar.Length - 7);

the_Info = " a " + " " + DRARName + " " + DFilePath; //命令 + 压缩后文件名 + 被压缩的文件或者路径

the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;

the_StartInfo.Arguments = the_Info;

the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

the_StartInfo.WorkingDirectory = DRARPath; //RaR文件的存放目录。

the_Process = new Process();

the_Process.StartInfo = the_StartInfo;

the_Process.Start();

the_Process.WaitForExit();//让用户等待进程结束,我这里是要同时下载,否则可以不用写这句

return true;

}

catch (Exception ex)

{

return false;

}

}

#endregion

#region 解压缩

/// <summary>

/// 解压缩到指定文件夹

/// </summary>

/// <param name="RARFilePath">压缩文件存在的目录 </param>

/// <param name="RARFileName">压缩文件名称 </param>

/// <param name="UnRARFilePath">解压到文件夹</param>

/// <returns></returns>

public static bool UnRAR(string RARFilePath, string RARFileName, string UnRARFilePath)

{

//解压缩

String the_rar;

RegistryKey the_Reg;

Object the_Obj;

String the_Info;

ProcessStartInfo the_StartInfo;

Process the_Process;

try

{

the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications/WinRar.exe/Shell/Open/Command");

the_Obj = the_Reg.GetValue("");

the_rar = the_Obj.ToString();

the_Reg.Close();

the_rar = the_rar.Substring(1, the_rar.Length - 7);

the_Info = @" X " + " " + RARFilePath + RARFileName + " " + UnRARFilePath;

the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;

the_StartInfo.Arguments = the_Info;

the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

the_Process = new Process();

the_Process.StartInfo = the_StartInfo;

the_Process.Start();

return true;

}

catch (Exception ex)

{

return false;

}

}

#endregion

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: