DirectoryServices管理配置IIS

DirectoryServices管理配置IIS


 using System;  
 using System.Collections.Generic;  
 using System.Text;  
 using System.DirectoryServices;  
 using System.Collections;  
 using Microsoft.Win32;  
 using System.Text.RegularExpressions;  
  
 namespace ConfigIIS  
 {  
     class Program  
     {  
         //  
         //  
         //  
         static void Main(string[] args)  
         {  
             if (((args.Length == 0) || args[0].Equals("/?")) || args[0].Equals("--help"))  
             {  
                 DisplayUsage();  
             }  
             else if (args.Length == 1)  
             {  
                 string webSiteNum = GetWebSiteNum(args[0].ToString());  
                 if (webSiteNum != null)  
                 {  
                     Console.WriteLine("网站编号:{0}", webSiteNum.ToString());  
                     SetSingleProperty("IIS://Localhost/W3SVC/" + webSiteNum + "/root", "ScriptMaps", "");  
                 }  
             }  
             else 
             {  
                 Console.WriteLine("不可识别的参数!");  
             }  
         }  
  
         //  
         // 设置站点属性   
         //  
         private static void SetSingleProperty(string metabasePath, string propertyName, object newValue)  
         {  
             try 
             {  
                 DirectoryEntry entry = new DirectoryEntry(metabasePath);  
                 ArrayList list = new ArrayList(entry.Properties[propertyName]);  
                 ArrayList list2 = new ArrayList();  
                 // HKEY_LOCAL_MACHINE\SYSTEM\LIWEIWENSOFT\INFOSAFE  
                 //RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\LIWEIWENSOFT\INFOSAFE");  
                 RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\LIWEIWENSOFT\INFOSAFE");  
                 object obj2 = key.GetValue("inidir");  
                 Console.WriteLine("7i24安装目录:{0}", obj2);  
                 if (key != null)  
                 {  
                     string str = obj2 + "infosafe.dll";  
                     string str2 = "*";  
                     string str3 = str;  
                     string str4 = "GET,HEAD,POST";  
                     string str5 = str2 + "," + str3 + ",1," + str4;  
                     Console.WriteLine("通配符映射字符串:{0}", str5.ToString());  
                     foreach (string str6 in list)  
                     {  
                         if (str6.Split(new char[] { ',' })[0].ToLower() != str2.ToLower())  
                         {  
                             list2.Add(str6);  
                         }  
                     }  
                     list2.Add(str5);  
                     entry.Properties[propertyName].Value = list2.ToArray();  
                     entry.CommitChanges();  
                     entry.Dispose();  
                 }  
             }  
             catch (Exception exception)  
             {  
                 if ("HRESULT 0x80005006" == exception.Message)  
                 {  
                     Console.WriteLine("属性 {0} 不存在,路径位置:{1}", propertyName, metabasePath);  
                 }  
                 else 
                 {  
                     Console.WriteLine("设置属性时出现如下错误信息: \n{0}", exception.Message);  
                 }  
             }  
         }  
  
         //  
         // 获取站点标识符  
         //   
         public static string GetWebSiteNum(string siteName)  
         {  
             Regex regex = new Regex(siteName);  
             DirectoryEntry entry = new DirectoryEntry(string.Format("IIS://Localhost/w3svc", new object[0]));  
             foreach (DirectoryEntry entry2 in entry.Children)  
             {  
                 if (entry2.SchemaClassName == "IIsWebServer")  
                 {  
                     string str;  
                     if (entry2.Properties["ServerBindings"].Value != null)  
                     {  
                         str = entry2.Properties["ServerBindings"].Value.ToString();  
                         if (regex.Match(str).Success)  
                         {  
                             return entry2.Name;  
                         }  
                     }  
                     if (entry2.Properties["ServerComment"].Value != null)  
                     {  
                         str = entry2.Properties["ServerComment"].Value.ToString();  
                         if (regex.Match(str).Success)  
                         {  
                             return entry2.Name;  
                         }  
                     }  
                 }  
             }  
             return null;  
         }  
  
         //  
         // 工具使用方法  
         //  
         private static void DisplayUsage()  
         {  
             Console.WriteLine("用法:");  
             Console.WriteLine("    ConfigIIS [/? | <DomainName>");  
             Console.WriteLine();  
             Console.WriteLine("这里:");  
             Console.WriteLine("    DomainName   主机域名.");  
             Console.WriteLine();  
             Console.WriteLine("命令行实例:");  
             Console.WriteLine("    设置指定域名站点的通配符映射      ConfigIIS 2mysite.net");  
         }  
     }  
 } 

发表评论