C#操作IIS类

ContractedBlock.gif ExpandedBlockStart.gif 操作IIS类
  1using System;
  2using System.DirectoryServices;
  3using System.Collections;
  4using System.Text.RegularExpressions;
  5using System.Text;
  6
  7namespace Business
  8ExpandedBlockStart.gifContractedBlock.gif{
  9    public class ControlIIS
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 11ContractedSubBlock.gifExpandedSubBlockStart.gif        UserName,Password,HostName的定义#region UserName,Password,HostName的定义
 12        public static string HostName
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 14            get
 15ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 16                return hostName;
 17            }

 18            set
 19ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 20                hostName = value;
 21            }

 22        }

 23        public static string UserName
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 25            get
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 27                return userName;
 28            }

 29            set
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 31                userName = value;
 32            }

 33        }

 34        public static string Password
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 36            get
 37ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 38                return password;
 39            }

 40            set
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 42                if (UserName.Length <= 1)
 43ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 44                    throw new ArgumentException("还没有指定好用户名。请先指定用户名");
 45                }

 46                password = value;
 47            }

 48        }

 49        public static void RemoteConfig(string hostName, string userName, string password)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 51            HostName = hostName;
 52            UserName = userName;
 53            Password = password;
 54        }

 55        private static string hostName = "localhost";
 56        private static string userName = "qf";
 57        private static string password = "qinfei";
 58        #endregion

 59
 60ContractedSubBlock.gifExpandedSubBlockStart.gif        根据路径构造Entry的方法#region 根据路径构造Entry的方法
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 62        /// 根据是否有用户名来判断是否是远程服务器。
 63        /// 然后再构造出不同的DirectoryEntry出来
 64        /// </summary>
 65        /// <param name="entPath">DirectoryEntry的路径</param>
 66        /// <returns>返回的是DirectoryEntry实例</returns>

 67        public static DirectoryEntry GetDirectoryEntry(string entPath)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 69            DirectoryEntry ent;
 70            if (UserName == null)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 72                ent = new DirectoryEntry(entPath);
 73            }

 74            else
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 76                ent = new DirectoryEntry(entPath, HostName + "\\" + UserName, Password, AuthenticationTypes.Secure);
 77                //ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
 78            }

 79            return ent;
 80        }

 81        #endregion

 82
 83ContractedSubBlock.gifExpandedSubBlockStart.gif        添加,删除网站的方法#region 添加,删除网站的方法
 84        public static void CreateNewWebSite(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 86            if (!EnsureNewSiteEnavaible(hostIP + portNum + descOfWebSite))
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 88                throw new ArgumentNullException("已经有了这样的网站了。" + Environment.NewLine + hostIP + portNum + descOfWebSite);
 89            }

 90
 91            string entPath = String.Format("IIS://{0}/w3svc", HostName);
 92            DirectoryEntry rootEntry = GetDirectoryEntry(entPath);//取得iis路径
 93            string newSiteNum = GetNewWebSiteID(); //取得新网站ID
 94            DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer"); //增加站点
 95            newSiteEntry.CommitChanges();//保存对区域的更改(这里对站点的更改)
 96            newSiteEntry.Properties["ServerBindings"].Value = hostIP + portNum + descOfWebSite;
 97            newSiteEntry.Properties["ServerComment"].Value = commentOfWebSite;
 98            newSiteEntry.CommitChanges();
 99            DirectoryEntry vdEntry = newSiteEntry.Children.Add("root""IIsWebVirtualDir");
100            vdEntry.CommitChanges();
101            vdEntry.Properties["Path"].Value = webPath;
102            vdEntry.CommitChanges();
103
104        }

105ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
106        /// 删除一个网站。根据网站名称删除。
107        /// </summary>
108        /// <param name="siteName">网站名称</param>

109        public static void DeleteWebSiteByName(string siteName)
110ExpandedSubBlockStart.gifContractedSubBlock.gif        {
111            string siteNum = GetWebSiteNum(siteName);
112            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
113            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
114            string rootPath = String.Format("IIS://{0}/w3svc", HostName);
115            DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);
116            rootEntry.Children.Remove(siteEntry);
117            rootEntry.CommitChanges();
118        }

119        #endregion

120
121ContractedSubBlock.gifExpandedSubBlockStart.gif        Start和Stop网站的方法#region Start和Stop网站的方法
122        public static void StartWebSite(string siteName)
123ExpandedSubBlockStart.gifContractedSubBlock.gif        {
124            string siteNum = GetWebSiteNum(siteName);
125            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
126            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
127ExpandedSubBlockStart.gifContractedSubBlock.gif            siteEntry.Invoke("Start"new object[] { });
128        }

129        public static void StopWebSite(string siteName)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        {
131            string siteNum = GetWebSiteNum(siteName);
132            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
133            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
134ExpandedSubBlockStart.gifContractedSubBlock.gif            siteEntry.Invoke("Stop"new object[] { });
135        }

136        #endregion

137
138ContractedSubBlock.gifExpandedSubBlockStart.gif        确认网站是否相同#region 确认网站是否相同
139ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
140        /// 确定一个新的网站与现有的网站没有相同的。
141        /// 这样防止将非法的数据存放到IIS里面去
142        /// </summary>
143        /// <param name="bindStr">网站邦定信息</param>
144        /// <returns>真为可以创建,假为不可以创建</returns>

145        public static bool EnsureNewSiteEnavaible(string bindStr)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        {
147            string entPath = String.Format("IIS://{0}/w3svc", HostName);
148            DirectoryEntry ent = GetDirectoryEntry(entPath);
149            foreach (DirectoryEntry child in ent.Children)
150ExpandedSubBlockStart.gifContractedSubBlock.gif            {
151                if (child.SchemaClassName == "IIsWebServer")
152ExpandedSubBlockStart.gifContractedSubBlock.gif                {
153                    if (child.Properties["ServerBindings"].Value != null)
154ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
155                        if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
156ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
157                            return false;
158                        }

159                    }

160                }

161            }

162            return true;
163        }

164
165        #endregion

166
167ContractedSubBlock.gifExpandedSubBlockStart.gif        获取一个网站编号//一个输入参数为站点描述#region 获取一个网站编号//一个输入参数为站点描述
168ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
169        /// 输入参数为 站点的描述名 默认是站点描述为 "默认网站"
170        /// </summary>
171        /// <param name="siteName">站点描述</param>
172        /// <exception cref="NotFoundWebSiteException">表示没有找到网站</exception>

173        public static string GetWebSiteNum(string siteName)
174ExpandedSubBlockStart.gifContractedSubBlock.gif        {
175            Regex regex = new Regex(siteName);
176            string tmpStr;
177            string entPath = String.Format("IIS://{0}/w3svc", HostName);
178            DirectoryEntry ent = GetDirectoryEntry(entPath);
179            foreach (DirectoryEntry child in ent.Children)
180ExpandedSubBlockStart.gifContractedSubBlock.gif            {
181                if (child.SchemaClassName == "IIsWebServer")
182ExpandedSubBlockStart.gifContractedSubBlock.gif                {
183                    if (child.Properties["ServerBindings"].Value != null)
184ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
185                        tmpStr = child.Properties["ServerBindings"].Value.ToString();
186                        if (regex.Match(tmpStr).Success)
187ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
188                            return child.Name;
189                        }

190                    }

191                    if (child.Properties["ServerComment"].Value != null)
192ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
193                        tmpStr = child.Properties["ServerComment"].Value.ToString();
194                        if (regex.Match(tmpStr).Success)
195ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
196                            return child.Name;
197                        }

198                    }

199                }

200            }

201            throw new Exception("没有找到我们想要的站点" + siteName);
202        }

203        #endregion

204
205ContractedSubBlock.gifExpandedSubBlockStart.gif        获取新网站id的方法#region 获取新网站id的方法
206ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
207        /// 获取网站系统里面可以使用的最小的ID。
208        /// 这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。
209        /// 这里面的算法经过了测试是没有问题的。
210        /// </summary>
211        /// <returns>最小的id</returns>

212        public static string GetNewWebSiteID()
213ExpandedSubBlockStart.gifContractedSubBlock.gif        {
214            ArrayList list = new ArrayList();
215            string tmpStr;
216            string entPath = String.Format("IIS://{0}/w3svc", HostName);
217            DirectoryEntry ent = GetDirectoryEntry(entPath);
218            foreach (DirectoryEntry child in ent.Children)
219ExpandedSubBlockStart.gifContractedSubBlock.gif            {
220                if (child.SchemaClassName == "IIsWebServer")
221ExpandedSubBlockStart.gifContractedSubBlock.gif                {
222                    tmpStr = child.Name.ToString();
223                    list.Add(Convert.ToInt32(tmpStr));
224                }

225            }

226            list.Sort();
227            int i = 1;
228            foreach (int j in list)
229ExpandedSubBlockStart.gifContractedSubBlock.gif            {
230                if (i == j)
231ExpandedSubBlockStart.gifContractedSubBlock.gif                {
232                    i++;
233                }

234            }

235            return i.ToString();
236        }

237        #endregion

238
239ContractedSubBlock.gifExpandedSubBlockStart.gif        增加,删除主机头#region 增加,删除主机头
240ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
241        /// 增加主机头
242        /// </summary>
243        /// <param name="HostName">站点描述</param>
244        /// <param name="ip">ip</param>
245        /// <param name="port">端口</param>
246        /// <param name="domain">域名</param>

247        public static void AddHostHeader(string HostName, string ip, int port, string domain)//增加主机头(站点编号.ip.端口.域名)
248ExpandedSubBlockStart.gifContractedSubBlock.gif        {
249            DirectoryEntry site = new DirectoryEntry(String.Format("IIS://{0}/w3svc", HostName));
250            PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
251            string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
252            if (!serverBindings.Contains(headerStr))
253ExpandedSubBlockStart.gifContractedSubBlock.gif            {
254                serverBindings.Add(headerStr);
255            }

256            site.CommitChanges();
257        }

258ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
259        /// 删除主机头
260        /// </summary>
261        /// <param name="HostName">站点描述</param>
262        /// <param name="ip">ip</param>
263        /// <param name="port">端口</param>
264        /// <param name="domain">域名</param>

265        public static void DeleteHostHeader(string HostName, string ip, int port, string domain)//删除主机头(站点编号.ip.端口.域名)
266ExpandedSubBlockStart.gifContractedSubBlock.gif        {
267            DirectoryEntry site = new DirectoryEntry(String.Format("IIS://{0}/w3svc", HostName));
268            PropertyValueCollection serverBindings = site.Properties["ServerBindings"];
269            string headerStr = string.Format("{0}:{1}:{2}", ip, port, domain);
270            if (serverBindings.Contains(headerStr))
271ExpandedSubBlockStart.gifContractedSubBlock.gif            {
272                serverBindings.Remove(headerStr);
273            }

274            site.CommitChanges();
275        }

276        #endregion

277
278    }

279}

280

转载于:https://www.cnblogs.com/lifenostink/archive/2009/02/13/1389928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值