Tuesday, 19 July 2011

Manipulate external app using ASP.NET 2.0 and VB.NET


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 VB.NET. 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 base Registrykeys 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.

Imports Microsoft.Win32;
Imports System.Diagnostics;

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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 Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim the_rar As String
Dim the_Reg As RegistryKey
Dim the_Obj As Object
Dim the_Info As String
Dim the_StartInfo As ProcessStartInfo
Dim the_Process As 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 ex As Exception
Response.Write("<script>alert('Zip Failed.')</script>")
End Try
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com
' Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim the_rar As String
Dim the_Reg As RegistryKey
Dim the_Obj As Object
Dim the_Info As String
Dim the_StartInfo As ProcessStartInfo
Dim the_Process As 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('Zip Successfully');</script>")
Catch ex As Exception
Response.Write("<script>alert('Zip Failed.')</script>")
End Try
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

The flow for the code behind page is as follows:

Imports Microsoft.Win32
Imports System.Diagnostics

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim the_rar As String
Dim the_Reg As RegistryKey
Dim the_Obj As Object
Dim the_Info As String
Dim the_StartInfo As ProcessStartInfo
Dim the_Process As 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 ex As Exception
Response.Write("<script>alert('Zip Failed.')</script>")
End Try
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim the_rar As String
Dim the_Reg As RegistryKey
Dim the_Obj As Object
Dim the_Info As String
Dim the_StartInfo As ProcessStartInfo
Dim the_Process As 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('Zip Successfully');</script>")
Catch ex As Exception
Response.Write("<script>alert('Zip Failed.')</script>")
End Try
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment