• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#操作IIS-App&AppPools

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  1 using System;
  2 using System.DirectoryServices;
  3 using System.Management;
  4 using Microsoft.Web.Administration;
  5 
  6 namespace ConsoleApplication_IsNullTest
  7 {
  8     /// <summary>
  9     /// IIS Helper.
 10     /// </summary>
 11     public class IISHelper
 12     {
 13 
 14         private static string siteName = "siteName";
 15         private static string sitePath = "sitePath";
 16         private static string serverId = "001";
 17         private static string runtimeVersion="v4.0";
 18 
 19         public static string RuntimeVersion
 20         {
 21             get { return runtimeVersion; }
 22         }
 23 
 24         public static DirectoryEntry iisSite = new DirectoryEntry("IIS://localhost/W3SVC", "UserName", "Userpwd");
 25 
 26         private static ManagementScope scope = null;
 27 
 28         public IISHelper()
 29         {
 30             scope = new ManagementScope("ConfigManager.ManagementScopePath");
 31             if (scope.IsConnected == false)
 32             {
 33                 scope.Connect();
 34             }
 35         }
 36 
 37         /// <summary>
 38         /// 创建应用程序池
 39         /// </summary>
 40         /// <param name="AppPoolName">程序池名</param>
 41         /// <param name="PipelineModel">Managed pipeline model.</param>
 42         public static void CreateAppPool(string AppPoolName, ManagedPipelineMode pipelineMode)
 43         {
 44             DirectoryEntry newpool;
 45             DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
 46             newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");
 47             newpool.Properties["ManagedRuntimeVersion"].Value = runtimeVersion;
 48             newpool.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated;
 49             newpool.CommitChanges();
 50         }
 51 
 52         /// <summary>
 53         /// 创建site
 54         /// </summary>
 55         public static void CreateWebSite(Object site)
 56         {
 57             ManagementObject manage = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
 58             ManagementBaseObject inputParameters = manage.GetMethodParameters("CreateNewSite");
 59             ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];
 60             serverBinding[0] = CreateServerBinding("", "127.0.0.1", "8081");
 61             inputParameters["ServerComment"] = siteName;
 62             inputParameters["ServerBindings"] = serverBinding;
 63             inputParameters["PathOfRootVirtualDir"] = sitePath;
 64             inputParameters["ServerId"] = serverId;
 65             try
 66             {
 67                 manage.InvokeMethod("CreateNewSite", inputParameters, null);
 68             }
 69             catch
 70             {
 71                 throw new Exception("createNewSite");
 72             }
 73             SetAppToPool(siteName, serverId);
 74 
 75             System.Threading.Thread.Sleep(3000);
 76 
 77             //启动web site
 78             string serverName = "W3SVC/" + serverId;
 79             ManagementObject manageSite = new ManagementObject(scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
 80             manageSite.InvokeMethod("Start", null);
 81         }
 82 
 83         /// <summary>
 84         /// 关联应用程序池
 85         /// </summary>
 86         /// <param name="appPoolName"></param>
 87         /// <param name="serverId"></param>
 88         private static void SetAppToPool(string appPoolName, string serverId)
 89         {
 90             string path = string.Format("IIS://localhost/W3SVC/{0}/Root", serverId);
 91             DirectoryEntry entry = new DirectoryEntry(path);
 92             entry.Properties["AppPoolId"].Value = appPoolName;
 93             entry.CommitChanges();
 94             entry.Close();
 95         }
 96 
 97         /// <summary>
 98         /// Get site url前缀
 99         /// </summary>
100         /// <param name="HostName"></param>
101         /// <param name="IP"></param>
102         /// <param name="Port"></param>
103         /// <returns></returns>
104         private static ManagementObject CreateServerBinding(string HostName, string IP, string Port)
105         {
106             try
107             {
108                 ManagementClass classBinding = new ManagementClass(scope, new ManagementPath("ServerBinding"), null);
109                 ManagementObject serverBinding = classBinding.CreateInstance();
110                 serverBinding.Properties["Hostname"].Value = HostName;
111                 serverBinding.Properties["IP"].Value = IP;
112                 serverBinding.Properties["Port"].Value = Port;
113                 serverBinding.Put();
114                 return serverBinding;
115             }
116             catch
117             {
118                 return null;
119             }
120         }
121 
122         /// <summary>
123         /// 创建web site 子项的site $ 应用程序池
124         /// </summary>
125         /// <param name="site"></param>
126         /// <param name="serviceName"></param>
127         /// <param name="AppolName"></param>
128         public static void CreateApplication(Object site, string serviceName, string AppolName)
129         {
130             DirectoryEntry defaultWebSite = GetWebisteDirectory("FUSiteName");//父级site name
131             if (defaultWebSite != null)
132             {
133                 DirectoryEntry defaultWebSiteRoot = new DirectoryEntry(defaultWebSite.Path + "/Root");
134 
135                 //Create and setup new virtual directory
136                 DirectoryEntry virtualDirectory = defaultWebSiteRoot.Children.Add(siteName, "IIsWebVirtualDir");
137 
138                 virtualDirectory.Properties["Path"][0] = sitePath;
139                 virtualDirectory.Properties["AppFriendlyName"][0] = siteName;
140                 virtualDirectory.CommitChanges();
141 
142                 virtualDirectory.Invoke("AppCreate", 1);
143                 object[] param = { 0, siteName, true };
144                 virtualDirectory.Invoke("AppCreate3", param);
145                 virtualDirectory.Properties["AppPoolId"].Value = AppolName;
146 
147                 string appPoolPath = @"IIS://localhost/W3SVC/AppPools/" + AppolName;
148                 try
149                 {
150                     var appPoolEntry = new DirectoryEntry(appPoolPath);
151                     appPoolEntry.Properties["ManagedRuntimeVersion"].Value = runtimeVersion;
152                     appPoolEntry.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated;
153                     appPoolEntry.Invoke("SetInfo", null);
154                     appPoolEntry.CommitChanges();
155                     appPoolEntry.Close();
156                 }
157                 catch
158                 {
159 
160                 }
161                 virtualDirectory.CommitChanges();
162                 virtualDirectory.Close();
163             }
164         }
165 
166 
167         /// <summary>
168         /// Get web site directory.
169         /// </summary>
170         /// <param name="websiteName"></param>
171         /// <returns></returns>
172         private static DirectoryEntry GetWebisteDirectory(string websiteName)
173         {
174             DirectoryEntries des = iisSite.Children;
175             foreach (DirectoryEntry sub in des)
176             {
177                 if (sub.SchemaClassName == "IIsWebServer" && sub.Properties["ServerComment"].Contains(websiteName.Trim()))
178                 {
179 
180                     return sub;
181                 }
182             }
183             return null;
184         }
185     }
186 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#拷贝文件夹A内的所有内容到B内发布时间:2022-07-13
下一篇:
C#为IE添加可信任站点发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap