经常会写一些测试用的片段 丢了又可惜 决定记下来

值类型检查

                 var list = new List<Type>()
                 {
                     typeof(Object),
                     typeof(DBNull),
                     typeof(Boolean),
                     typeof(Char),
                     typeof(SByte),
                     typeof(Byte),
                     typeof(Int16),
                     typeof(UInt16),
                     typeof(Int32),
                     typeof(UInt32),
                     typeof(Int64),
                     typeof(UInt64),
                     typeof(Single),
                     typeof(Double),
                     typeof(Decimal),
                     typeof(DateTime),
                     typeof(String),
                 };
                 foreach(var item in list)
                 {
                     WL("Type: {0}, IsValueType: {1}", item.Name, item.IsValueType);
                 }

 IIS 管理  C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll

创建 删除 修改 虚拟目录

        static void UpdateIIS(string[] args)
        {
            using (var serverManager = new ServerManager())
            {
#if !DEBUG
                if (args.Length != 3)
                {
                    Console.Write("参数不正确!");
                    return;
                }
#endif

                Configuration config = serverManager.GetApplicationHostConfiguration();
                ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
                ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
#if !DEBUG
                ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", args[0]);
#else
                ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", "LXT");
#endif
                if (siteElement == null)
                {
                    Console.WriteLine("IIS 未找到名为 {0} 的站点", args[0]);
                    return;
                }


                ConfigurationElementCollection siteCollection = siteElement.GetCollection();

#if !DEBUG
                Delete(siteCollection, args[1]);
                Create(siteCollection, args[1], args[2]);
#else
                Delete(siteCollection, "/HeadImages");
                Create(siteCollection, "/HeadImages", @"D:\Dev\IIS7");
#endif

                serverManager.CommitChanges();

            }
            Console.WriteLine("IIS 更新完成");
        }


        static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
        {
            foreach (ConfigurationElement element in collection)
            {
                if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
                {
                    bool matches = true;
                    for (int i = 0; i < keyValues.Length; i += 2)
                    {
                        object o = element.GetAttributeValue(keyValues[i]);
                        string value = null;
                        if (o != null)
                        {
                            value = o.ToString();
                        }
                        if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
                        {
                            matches = false;
                            break;
                        }
                    }
                    if (matches)
                    {
                        return element;
                    }
                }
            }
            return null;
        }

        static void Delete(ConfigurationElementCollection collection, string virtualDirectoryPath)
        {
            var oldApplication = collection.SingleOrDefault(e => e.Attributes["path"].Value.ToString() == "/");
            if (oldApplication == null) return;
            var virtualDirectorys = oldApplication.GetCollection();
            if (virtualDirectorys.Count == 1) return;
            var virtualDirectory = virtualDirectorys.SingleOrDefault(e => e.Attributes["path"].Value.ToString() == virtualDirectoryPath);
            if (virtualDirectory == null) return;
            virtualDirectorys.Remove(virtualDirectory);
            Console.WriteLine("IIS 删除 虚拟目录");
        }

        static void Create(ConfigurationElementCollection collection, string virtualDirectoryPath, string physicalPath)
        {

            var application = collection.SingleOrDefault(e => e.Attributes["path"].Value.ToString() == virtualDirectoryPath);
            if (application == null)
            {
                application = collection.CreateElement("application");
                application["path"] = virtualDirectoryPath;
                collection.Add(application);
            }
            application["applicationPool"] = "LXT";

            ConfigurationElementCollection virtualDirectorys = application.GetCollection();
            var virtualDirectory = virtualDirectorys.SingleOrDefault(e => e.Attributes["path"].Value.ToString() == "/");
            if (virtualDirectory == null)
            {
                virtualDirectory = virtualDirectorys.CreateElement("virtualDirectory");
                virtualDirectory["path"] = @"/";
                virtualDirectorys.Add(virtualDirectory);
                Console.WriteLine("IIS 创建 应用程序");
            }
            else
                Console.WriteLine("IIS 修改 应用程序");
            virtualDirectory["physicalPath"] = physicalPath;
        }
posted on 2011-02-24 13:28  jayu  阅读(281)  评论(0编辑  收藏  举报