xinghe0 发表于 2015-8-16 11:12:54

IIS开发操作(安装包调用)

引用:

using System.DirectoryServices;

获取虚拟目录或网站:
  

      private DirectoryEntry IsExistVDir(string hostName, string vDirName, out int webSiteNum)
      {
            DirectoryEntry rootEntry = new DirectoryEntry("IIS://" + hostName + "/W3SVC");
            webSiteNum = 1;
            //定位查找已存在的目录
            foreach (DirectoryEntry entry in rootEntry.Children)    //网站
            {
                if (entry.SchemaClassName.ToLower() != "IIsWebServer".ToLower()) continue;
                foreach (DirectoryEntry en in entry.Children)       //虚拟目录
                {
                  if (en.Name.ToLower() == "root")
                        foreach (DirectoryEntry subEn in en.Children)
                            if (subEn.Name == vDirName)
                              return subEn;
                }
                //在这一级可以判断网站(关键词可以用 ServerComment),
                webSiteNum++;
            }

            webSiteNum--;
            return null;
      }
获取已存在的虚拟目录或Web属性, 如: IP,PORT,HEADER

      private string getIP_PORT_HEADER(string hostName, string vDirName)
      {
            DirectoryEntry VDir;
            int webSiteNum;
            VDir = IsExistVDir(hostName, vDirName, out webSiteNum);
            if (VDir == null)   //不存在
            {
                //"不存在的<虚拟目录名称>"
                return null;
            }
            //LocalHost/W3SVC/{webSiteNum}/Root/{vDirName} 规则
            //VDir={vDirName}当前网站, {webSiteNum}为当前虚拟目录

            //PropertyValueCollection pvc = VDir.Parent.Parent.Properties["ServerBindings"];
            DirectoryEntry rootEntry = new DirectoryEntry("IIS://" + hostName + "/W3SVC/" + webSiteNum);
            PropertyValueCollection pvc = rootEntry.Properties["ServerBindings"];

            return pvc.ToString();
      }
应用程序扩展映射

       /// <summary>
      /// Add ISAPI Map
      /// </summary>
      /// <param name="hostName">host name</param>
      /// <param name="vDirName">virtual directory name</param>
      /// <param name="isAddOrDel">add is true, del is false</param>
      private void CreateMap(string hostName, string vDirName, optMethod opt)
      {
            //要新增的映射,默认执行应用程序指向aspnet_isapi.dll
            string[] exts = new string[] { ".rest", ".nav" };
            DirectoryEntry VDir;

            int webSiteNum;
            VDir = IsExistVDir(hostName, vDirName, out webSiteNum);
            if (VDir == null)   //不存在
            {
                MessageBox.Show("不存在的<虚拟目录名称>");
                return;
            }

            ArrayList orglist = new ArrayList(VDir.Properties["ScriptMaps"]);
            ArrayList newlist = new ArrayList();

            NameValueCollection extMaps = GetExtMaps(exts);

            foreach (string s in orglist)
            {
                string[] tokn = s.Split(',');
                bool hasExt = false;
                foreach (string extmap in extMaps.AllKeys)
                {
                  if (tokn.ToLower() == extmap.ToLower())
                  {
                        hasExt = true;
                  }
                }
                if (!hasExt)
                  newlist.Add(s);
            }
            switch (opt)
            {
                case optMethod.Add:
                  foreach (string extmap in extMaps.AllKeys)
                  {
                        newlist.Add(extMaps);
                  }
                  break;
                case optMethod.Remove:
                  break;
                case optMethod.Update:
                  foreach (string extmap in extMaps.AllKeys)
                  {
                        newlist.Add(extMaps);
                  }
                  break;
            }

            try
            {
                VDir.Properties["ScriptMaps"].Value = newlist.ToArray();
                VDir.CommitChanges();
                MessageBox.Show("操作成功");
                VDir.Dispose();
            }
            catch (Exception ex)
            {
                VDir.Dispose();
                MessageBox.Show("操作出错: " + ex.Message);
            }
      }

      private NameValueCollection GetExtMaps(string[] exts)
      {
            //string rootpath = Environment.GetFolderPath(Environment.SpecialFolder.System);
            string sysRootPath = Environment.GetEnvironmentVariable("windir");

            string path = sysRootPath + "\\Microsoft.NET\\Framework\\v2.0.50727\\aspnet_isapi.dll";
            string methods = "";      //"GET,HEAD,POST,DEBUG"
            string newmap = "," + path + ",5," + methods;
            NameValueCollection extMaps = new NameValueCollection();
            foreach (string ext in exts)
            {
                extMaps.Add(ext, ext + newmap);
            }
            return extMaps;
      }
    }

/// <summary>
    /// ext操作
    /// </summary>
    enum optMethod
{
      Add,
      Remove,
      Update
    }映射调用

//新增
CreateMap("LocalHost", "VirtualDir", optMethod.Add);
//移除
CreateMap("LocalHost", "VirtualDir", optMethod.Remove);
页: [1]
查看完整版本: IIS开发操作(安装包调用)