设为首页 收藏本站
查看: 992|回复: 0

[经验分享] C#操作IIS类

[复制链接]

尚未签到

发表于 2015-8-13 07:57:24 | 显示全部楼层 |阅读模式
DSC0000.gif DSC0001.gif 操作IIS类
  1 DSC0002.gif using System;
  2using System.DirectoryServices;
  3using System.Collections;
  4using System.Text.RegularExpressions;
  5using System.Text;
  6
  7namespace Business
  8 DSC0003.gif {
  9 DSC0004.gif     public class ControlIIS
10 DSC0005.gif DSC0006.gif     {
11        UserName,Password,HostName的定义#region UserName,Password,HostName的定义
12        public static string HostName
13        {
14            get
15            {
16                return hostName;
17 DSC0007.gif             }
18            set
19            {
20                hostName = value;
21            }
22        }
23        public static string UserName
24        {
25            get
26            {
27                return userName;
28            }
29            set
30            {
31                userName = value;
32            }
33        }
34        public static string Password
35        {
36            get
37            {
38                return password;
39            }
40            set
41            {
42                if (UserName.Length <= 1)
43                {
44                    throw new ArgumentException("还没有指定好用户名。请先指定用户名");
45                }
46                password = value;
47            }
48        }
49        public static void RemoteConfig(string hostName, string userName, string password)
50        {
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
60        根据路径构造Entry的方法#region 根据路径构造Entry的方法
61        /**//// <summary>
62        /// 根据是否有用户名来判断是否是远程服务器。
63        /// 然后再构造出不同的DirectoryEntry出来
64        /// </summary>
65        /// <param name="entPath">DirectoryEntry的路径</param>
66        /// <returns>返回的是DirectoryEntry实例</returns>
67        public static DirectoryEntry GetDirectoryEntry(string entPath)
68        {
69            DirectoryEntry ent;
70            if (UserName == null)
71            {
72                ent = new DirectoryEntry(entPath);
73            }
74            else
75            {
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
83        添加,删除网站的方法#region 添加,删除网站的方法
84        public static void CreateNewWebSite(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)
85        {
86            if (!EnsureNewSiteEnavaible(hostIP + portNum + descOfWebSite))
87            {
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        }
105        /**//// <summary>
106        /// 删除一个网站。根据网站名称删除。
107        /// </summary>
108        /// <param name="siteName">网站名称</param>
109        public static void DeleteWebSiteByName(string siteName)
110        {
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
121        Start和Stop网站的方法#region Start和Stop网站的方法
122        public static void StartWebSite(string siteName)
123        {
124            string siteNum = GetWebSiteNum(siteName);
125            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
126            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
127            siteEntry.Invoke("Start", new object[] { });
128        }
129        public static void StopWebSite(string siteName)
130        {
131            string siteNum = GetWebSiteNum(siteName);
132            string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);
133            DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);
134            siteEntry.Invoke("Stop", new object[] { });
135        }
136        #endregion
137
138        确认网站是否相同#region 确认网站是否相同
139        /**//// <summary>
140        /// 确定一个新的网站与现有的网站没有相同的。
141        /// 这样防止将非法的数据存放到IIS里面去
142        /// </summary>
143        /// <param name="bindStr">网站邦定信息</param>
144        /// <returns>真为可以创建,假为不可以创建</returns>
145        public static bool EnsureNewSiteEnavaible(string bindStr)
146        {
147            string entPath = String.Format("IIS://{0}/w3svc", HostName);
148            DirectoryEntry ent = GetDirectoryEntry(entPath);
149            foreach (DirectoryEntry child in ent.Children)
150            {
151                if (child.SchemaClassName == "IIsWebServer")
152                {
153                    if (child.Properties["ServerBindings"].Value != null)
154                    {
155                        if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
156                        {
157                            return false;
158                        }
159                    }
160                }
161            }
162            return true;
163        }
164
165        #endregion
166
167        获取一个网站编号//一个输入参数为站点描述#region 获取一个网站编号//一个输入参数为站点描述
168        /**//// <summary>
169        /// 输入参数为 站点的描述名 默认是站点描述为 "默认网站"
170        /// </summary>
171        /// <param name="siteName">站点描述</param>
172        /// <exception cref="NotFoundWebSiteException">表示没有找到网站</exception>
173        public static string GetWebSiteNum(string siteName)
174        {
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)
180            {
181                if (child.SchemaClassName == "IIsWebServer")
182                {
183                    if (child.Properties["ServerBindings"].Value != null)
184                    {
185                        tmpStr = child.Properties["ServerBindings"].Value.ToString();
186                        if (regex.Match(tmpStr).Success)
187                        {
188                            return child.Name;
189                        }
190                    }
191                    if (child.Properties["ServerComment"].Value != null)
192                    {
193                        tmpStr = child.Properties["ServerComment"].Value.ToString();
194                        if (regex.Match(tmpStr).Success)
195                        {
196                            return child.Name;
197                        }
198                    }
199                }
200            }
201            throw new Exception("没有找到我们想要的站点" + siteName);
202        }
203        #endregion
204
205        获取新网站id的方法#region 获取新网站id的方法
206        /**//// <summary>
207        /// 获取网站系统里面可以使用的最小的ID。
208        /// 这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。
209        /// 这里面的算法经过了测试是没有问题的。
210        /// </summary>
211        /// <returns>最小的id</returns>
212        public static string GetNewWebSiteID()
213        {
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)
219            {
220                if (child.SchemaClassName == "IIsWebServer")
221                {
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)
229            {
230                if (i == j)
231                {
232                    i++;
233                }
234            }
235            return i.ToString();
236        }
237        #endregion
238
239        增加,删除主机头#region 增加,删除主机头
240        /**//// <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.端口.域名)
248        {
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))
253            {
254                serverBindings.Add(headerStr);
255            }
256            site.CommitChanges();
257        }
258        /**//// <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.端口.域名)
266        {
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))
271            {
272                serverBindings.Remove(headerStr);
273            }
274            site.CommitChanges();
275        }
276        #endregion
277
278    }
279 DSC0008.gif }
280

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-98161-1-1.html 上篇帖子: IIS无法启动:发生意外错误0x8ffe2740的原因 下篇帖子: 修改IIS设置延长DEBUG时间
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表