Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Start or Stop IIS Service in C#.NET, VB.NET using Asp.net

Feb 9, 2015
Introduction

Here I will explain how to start or stop IIS service in
c#, vb.net using asp.net. We can start or stop IIS service using ServiceController in c#, vb.net using asp.net.

Description:
  
In previous articles I explained check whether IIS service running or not in c#, vb.net,  
install iis manger in windows, IIS cannot open w3svc service on computer, host website in IIS with custom url in asp.net and many articles relating to asp.net, c#, vb.net, jQuery and JavaScript and. Now I will how to start or stop IIS service in c#, vb.net using asp.net.

If you want to start or stop IIS service in c#, vb.net using asp.net we need to write the code like as shown below

C# Code


protected void btnClick_Click(object sender, EventArgs e)
{
ServiceController controller = new ServiceController("W3SVC");
if (controller.Status == ServiceControllerStatus.Running)
controller.Stop();
else if (controller.Status == ServiceControllerStatus.Stopped)
controller.Start();
else
controller.Stop();
}
VB.NET Code


Protected Sub btnClick_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim controller As New ServiceController("W3SVC")
If controller.Status = ServiceControllerStatus.Running Then
controller.[Stop]()
ElseIf controller.Status = ServiceControllerStatus.Stopped Then
controller.Start()
Else
controller.[Stop]()
End If
End Sub
If you want to see it in complete example open your Default.aspx page and write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Start or Stop IIS service in Asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnClick" runat="server" Text="Check Status"
onclick="btnClick_Click" />
</div>
</form>
</body>
</html>
Now we need to add System.ServiceProcess reference to our application for that right click on your application à select Add Reference à select System.ServiceProcess like as shown below


Now open your code behind file and add following namespaces

C# Code


using System;
using System.ServiceProcess;
VB.NET Code


Imports System.ServiceProcess
Now write the following code in code behind to check the status of IIS manager

C# Code


protected void btnClick_Click(object sender, EventArgs e)
{
ServiceController controller = new ServiceController("W3SVC");
if (controller.Status == ServiceControllerStatus.Running)
controller.Stop();
else if (controller.Status == ServiceControllerStatus.Stopped)
controller.Start();
else
controller.Stop();
}
VB.NET Code


Imports System.ServiceProcess
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Protected Sub btnClick_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim controller As New ServiceController("W3SVC")
If controller.Status = ServiceControllerStatus.Running Then
controller.[Stop]()
ElseIf controller.Status = ServiceControllerStatus.Stopped Then
controller.Start()
Else
controller.[Stop]()
End If
End Sub
End Class
By using above code we can start or stop IIS service…. I hope it helps you….

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Unknown said...
This comment has been removed by the author.

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.