代码需要引入一个 Microsoft.Web.Administration.dll,文件位置在 \Windows\System32\inetsrv 目录。
核心代码:
/// <summary> /// 关闭其它站点,只开启输入名称的站点 /// </summary> /// <param name="startSiteName"></param> private static void StartWebsite(string startSiteName) { var webManager = new Microsoft.Web.Administration.ServerManager(); var startSite = webManager.Sites[startSiteName]; if (startSite == null) { Console.WriteLine("Can't not find site:{0}", startSiteName); return; } foreach (var site in webManager.Sites) { if (site.Name != startSiteName) site.Stop(); } startSite.Start(); }
IIS6.0 的停止和启动方法参考
http://www.cnblogs.com/yahle/archive/2008/03/25/1121518.html