Invoke Batch Program to Start And Stop Application Pool From C#

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 8325 red flag

A recent requirement in the project made us to write this article.The requirement was to Start And Stop Application Pool From C#. We have create a batch program to do the operation and invoke the same from C#


 Download source code for Invoke Batch Program to Start And Stop Application Pool From C#

Recommendation
Read Let's Explore Diagnostics in ASP.NET Core 1.0 before this article.

Introduction

A recent requirement in the project made us to write this article.The requirement was to Start And Stop Application Pool From C#. We have create a batch program to do the operation and invoke the same from C#. We are not claiming that this is the only way to do it, but however, it is a way to do so. So let's start.

Let's start the experiment

Let's first create a website in IIS say ExampleSiteForTest

Next create an Aplication Pool say ExampleApplicationPool

Now we need to add the ExampleSiteForTest to the ExampleApplicationPool. Choose the ExampleSiteForTest first. In the Actions pane, click Basic Settings to open the Edit Site dialog box.

Now at present the ExampleApplicationPool status is Started

Let us create a StopAppPool.bat file and write the below DOS command.

@ECHO ----------- About to stop the ExampleApplicationPool-----------------
cd %windir%\system32\inetsrv 
appcmd stop apppool /apppool.name:ExampleApplicationPool
@ECHO ----------- ExampleApplicationPool Stopped-----------------

To stop an application pool, we use the following syntax

appcmd stop apppool /apppool.name: string

where the parameter string is the name of the application pool that we need to stop.

Now let's execute the batch file (StopAppPool.bat) and the result is as under

Let us create another batch file StartAppPool.bat file and write the below DOS command.

@ECHO ----------- About to start the ExampleApplicationPool-----------------
cd %windir%\system32\inetsrv 
appcmd start apppool /apppool.name:ExampleApplicationPool
@ECHO ----------- ExampleApplicationPool Started-----------------

To start an application pool, we use the following syntax

appcmd start apppool /apppool.name: string

where the parameter string is the name of the application pool that we need to stop.

Now let's execute the batch file (StartAppPool.bat) and the result is as under

As a final activity, we need to write a C# code to invoke the batch program. Let's look at the program below which is written in a console application

using System;
using System.Diagnostics;

namespace ConsoleApplication1
{
    class Program
    {
        const string StartAppPoolFile = "StartAppPool.bat";
        const string StopAppPoolFile = "StopAppPool.bat";
        const string WorkingDirectory = @"C:\";

        static void Main(string[] args)
        {
            Console.WriteLine("1.Start Application Pool");
            Console.WriteLine("2.Stop Application Pool");
            Console.WriteLine("Enter Your Choice 1 OR 2");
            var choice = Console.ReadLine();

            var execute = choice == "1" ? StartAppPool() : StopAppPool();

            if(execute == 0) Console.WriteLine("Batch executed successfully");
            else Console.WriteLine("Some problem happened while executing the batch program");

            Console.ReadKey();
        }       

        private static int StartAppPool()
        {
            return ExecuteBatchProgram(StartAppPoolFile);
        }

        private static int StopAppPool()
        {
            return ExecuteBatchProgram(StopAppPoolFile);
        }

        private static int ExecuteBatchProgram(string fileName)
        {
            var exitCode = -1;
            using (Process proc = new Process())
            {
                //Set the working directory for the process to be started
                proc.StartInfo.WorkingDirectory = WorkingDirectory;

                //Set the doucment name to start
                proc.StartInfo.FileName = fileName;

                //Execute the process
                proc.Start();

                //Wait for the associated process to exit
                proc.WaitForExit();

                //Gets the value of the associated process specified when it terminated.
                exitCode = proc.ExitCode;
            }
            return exitCode;
        }
    }
}

After we run the application, we will get the below output

N.B.~ If anyone wants to start and stop the website, then the commands are

  1. Start Site Command:
    appcmd start site /site.name:ExampleSiteForTest
  2. Stop Site Command:
    appcmd stop site /site.name:ExampleSiteForTest

References

Start or Stop an Application Pool (IIS 7)

Conclusion

In this article, we have learnt about the invocation of batch file to Start And Stop Application Pool from C#. Hope this will be helpful. Thanks for reading. Zipped file attached.

Recommendation
Read Let's Explore ASP.NET Core 1.0 briefly after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)