Drakoo
Drakoo

Reputation: 327

Start Stop IIS Application Pool

I found this code:

using System.DirectoryServices;

...

void Recycle(string appPool)
{
    string appPoolPath = "IIS://servername/W3SVC/AppPools/" + appPool;

    using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath))
    {
        appPoolEntry.Invoke("Recycle", null);
        appPoolEntry.Close();
    }
}

But when I try to use this code I have this error:

Exception has been thrown by the target of an invocation., StackTrace: at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

What I'm doing wrong?
or how can get info about status of my application pool, and how can I start and stop my app pool without any special permissions?

I'am using built-in account: NetworkService

Upvotes: 4

Views: 7923

Answers (1)

Idan Levi
Idan Levi

Reputation: 458

Try to use Microsoft.Web.Administration.ServerManager and Microsoft.Web.Administration.ApplicationPool classes.

Example:

var serverManager = new ServerManager();
var appPool = serverManager.ApplicationPools.FirstOrDefault(ap => ap.Name.Equals("AppPoolName"));
appPool.Start();

Upvotes: 4

Related Questions