Words on my addictions: .NET, Philosophy, Music

25 May 2006

Stop & Start IIS Service

Filed under: C# — stefanprodan @ 2:53 am
private void StopIIS() 
{ 
    ServiceController iis = new ServiceController("W3SVC"); 
    if (null != iis) 
    { 
        do 
        { 
            iis.Refresh(); 
        } 
        while 
            ( 
            iis.Status == 
            ServiceControllerStatus.ContinuePending || 
            iis.Status == 
            ServiceControllerStatus.PausePending || 
            iis.Status == 
            ServiceControllerStatus.StartPending || 
            iis.Status == 
            ServiceControllerStatus.StopPending 
            ); 
        if (ServiceControllerStatus.Running == 
            iis.Status || 
            ServiceControllerStatus.Paused == iis.Status) 
        { 
            iis.Stop(); 
            iis.WaitForStatus( 
                ServiceControllerStatus.Stopped); 
        } 
        iis.Close(); 
    } 
} 

private void StartIIS() 
{ 
     ServiceController iis = new ServiceController("W3SVC"); 
     if (null != iis) 
     { 
         do 
         { 
             iis.Refresh(); 
         } 
         while 
             ( 
             iis.Status == 
             ServiceControllerStatus.ContinuePending || 
             iis.Status == 
             ServiceControllerStatus.PausePending || 
             iis.Status == 
             ServiceControllerStatus.StartPending || 
             iis.Status == 
             ServiceControllerStatus.StopPending 
             ); 
         if (ServiceControllerStatus.Stopped == iis.Status) 
         { 
             iis.Start(); 
             iis.WaitForStatus( 
                 ServiceControllerStatus.Running); 
         } 
         else 
         { 
             if (ServiceControllerStatus.Paused == 
                 iis.Status) 
             { 
                 iis.Continue(); 
                 iis.WaitForStatus( 
                     ServiceControllerStatus.Running); 
             } 
         } 
         iis.Close(); 
     } 
}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: