C++C#等编程控制IIS进程池
原创C++
//C++ adsi,
//引入activeds.lib;adsiid.lib两个库文件
//iis10不支持,报告错误ADsGetObject() failed. Error 0x80004005
//iis7.5支持
#include
#include
#include
#include
#include
#include
#include
int main()
{
HRESULT hr;
IADs* pADsPool = NULL;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
wprintf(L"CoInitialize failed. Error 0x%0x
", hr); return; }
hr = ADsGetObject(L"IIS://localhost/w3svc/AppPools/MyTest", IID_IADs, (void**)&pADsPool);
if (FAILED(hr))
{
wprintf(L"ADsGetObject() failed. Error 0x%0x
", hr); return; }
IISApplicationPool* appPool = NULL;
hr = pADsPool->QueryInterface(IID_IISApplicationPool, (void**)&appPool);
if (hr == S_OK)
{
//appPool->Stop();
//appPool->Start();
appPool->Recycle();
printf("OK");
}
CoUninitialize();
system("pause");
}
C#
//C# 使用Microsoft.Web.Administration;
//添加引用、浏览和C:WindowsSystem32inetsrv下选中Microsoft.Web.Administration.dll
//iis7.5可用
//iis10 win10可用,但需要管理权限。双击以未经许可运行。右键单击管理员以运行
using Microsoft.Web.Administration;
ServerManager sm = new ServerManager();
try
{
var pool = sm.ApplicationPools["MyAPP"];
if (pool == null)
{
Console.WriteLine("pool == null");
return;
}
if (pool.State == ObjectState.Started)
{
Console.WriteLine("is start, now stop");
if (pool.Stop() == ObjectState.Stopped)
{
Console.WriteLine("已成功停止应用程序池");
}
else
{
Console.WriteLine("停止失败");
}
}
else
{
Console.WriteLine("is stop, now start");
if (pool.Start() == ObjectState.Started)
{
Console.WriteLine("已成功启动应用程序池" + AppPoolName);
}
else
{
Console.WriteLine("启动失败");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
GC.Collect();
Console.Read();
C#
//C# 使用System.DirectoryServices 实际上,它也被使用ADSI的com组件
//iis7.5可用,iis10不可用
//添加引用、装配体、System.DirectoryServices
using System.DirectoryServices;
try
{
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc/AppPools/MyAPP");
//w3svc.Invoke("Recycle", null);
w3svc.Invoke("Start", null);
w3svc.Invoke("Stop", null);
Console.WriteLine("执行成功");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
Console.Read();
//bat脚本控制
cd %windir%system32inetsrv
appcmd recycle apppool MyAPP
appcmd stop apppool /apppool.name:MyAPP
appcmd start apppool /apppool.name:MyAPP
或全路径
c:windowssystem32inetsrvAppCmd.exe stop apppool /apppool.name:"MyAPP"
c:windowssystem32inetsrvAppCmd.exe start apppool /apppool.name:"MyAPP"
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
上一篇:白话快速排序 下一篇:可以在线C++编译的工具站点