1.前台设计页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Agent_Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>服务器端解压缩 </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="压缩" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="解压缩" /></div>
</form>
</body>
</html>
2.后台.cs页面
引入空间
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;
protected void Button1_Click(object sender, EventArgs e)
{
//压缩
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");
//调用winner命令,此句不可更改
the_Obj = the_Reg.GetValue("");
//得到winner的对象。此句不可更改
the_rar = the_Obj.ToString();
//将此对象转换为实体。表示实际的对象。
Response.Write(the_rar+"<br>");
//值为"C:/Program Files/WinRAR/WinRAR.exe" "%1"
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
//获取winner对象的实体的类型
Response.Write(the_rar);
//值为:C:/Program Files/WinRAR/WinRAR.exe
the_Info = " a " + " 1.rar " + " "+ @"C:/1/1.txt";
//得到转换文件的对象,格式: //命令(不可更改) + 压缩后文件名 + 被压缩的文件或者路径
the_StartInfo = new ProcessStartInfo();
//初始化进程对象
the_StartInfo.FileName = the_rar;
//得到初始化进程对象文件名
the_StartInfo.Arguments = the_Info;
//传入对象的参数
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//指定系统启动进程是的窗口状态
the_StartInfo.WorkingDirectory =@"C:/1";
//获取或设置要启动的进程的初始目录。
the_Process = new Process();
//进程初始化开始对象实例
the_Process.StartInfo = the_StartInfo;
//传递进程的属性
the_Process.Start();
//开始压缩
Response.Write("压缩成功");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//解压缩
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 " + " 1.rar " + " " + @"C:/1";
//得到转换文件的对象,格式: //命令(不可更改) + 压缩后文件名 + 被压缩的文件或者路径
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();
Response.Write("解压缩成功");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}