Manipulate external app using ASP.NET 2.0 and C#

This tutorial will use WinRAR as an example to show you how to excute an outside application process by using ASP.NET 2.0 and C#. We will manipulate WinRAR command to compress or extract file. 


This tutorial will use WinRAR as an example to show you how to excute an outside application process by using ASP.NET 2.0 and C#. We will manipulate WinRAR command to compress or extract file.
At first, you need to import the namespace from Microsoft.Win32 and System.Diagnostics.RegistryKey and Registry class in the namespace Microsoft.Win32.Class RegistryKey represents a key-level node in the Windows registry, which is a registry encapsulation.Class Registry Supplies the baseRegistrykeys that access values and subkeys in the registry.ProcessStartInfo and Process class in the namespace System.Diagnostics. ProcessStartInfo is used in conjunction with the Process component. When you start a process using the Process class, you have access to process information in addition to that available when attaching to a running process.Process provides access to local and remote processes and enables you to start and stop local system processes.
using Microsoft.Win32;
using System.Diagnostics;
We used over 10 web hosting companies before we found Server Intellect. Their dedicated serversand add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
In order to run the example, please create a file under the directory c:\test\test.txt. Run the example and click Zip button you will see a compressed file test.rar will be generated in the c:\test .Click UnZip button you will see that the test.rar will be extracted. Button1_Click event is used to zip a file and Button2_Click is used to unzip a file. The following code is:
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");
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 " + " test.rar " + " " + @"C:\test\test.txt";
the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\test\";
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("<script>alert('Zip Successfully');</script>");
}
catch
{
Response.Write("<script>alert('Zip Failed.')</script>");
}
}

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 " + " test.rar " + @"C:\test\";
the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\test\";
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("<script>alert('UnZip Successfully');</script>");
}
catch
{
Response.Write("<script>alert('UnZip Failed.')</script>");
}
}
Try Server Intellect for Windows Server Hosting. Quality and Quantity!
The front end page looks something like this: 

<div align="center">
<asp:Button ID="Button1" runat="server" Text="Zip" Width="71px" OnClick="Button1_Click" />
&nbsp; &nbsp; &nbsp;<asp:Button ID="Button2" runat="server" Text="UnZip" Width="76px" OnClick="Button2_Click" />
</div>
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The flow for the code behind page is as follows:

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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

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");
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 " + " test.rar " + " " + @"C:\test\test.txt";
the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\test\";
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("<script>alert('Zip Successfully');</script>");
}
catch
{
Response.Write("<script>alert('Zip Failed.')</script>");
}
}

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 " + " test.rar " + @"C:\test\";
the_StartInfo = new ProcessStartInfo();

the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = @"C:\test\";
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
Response.Write("<script>alert('UnZip Successfully');</script>");
}
catch
{
Response.Write("<script>alert('UnZip Failed.')</script>");
}
}
}

Comments