• 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[0].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[0].ToLower() == extmap.ToLower())
                        
    {
                            hasExt 
    = true;
                        }

                    }

                    
    if (!hasExt)
                        newlist.Add(s);
                }

                
    switch (opt)
                
    {
                    
    case optMethod.Add:
                        
    foreach (string extmap in extMaps.AllKeys)
                        
    {
                            newlist.Add(extMaps[extmap]);
                        }

                        
    break;
                    
    case optMethod.Remove:
                        
    break;
                    
    case optMethod.Update:
                        
    foreach (string extmap in extMaps.AllKeys)
                        
    {
                            newlist.Add(extMaps[extmap]);
                        }

                        
    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);
  • 相关阅读:
    C#基础知识——类的继承
    值传递与引用传递01
    今天接到任务了!
    傅立叶变换,时域,频域二
    傅立叶变换,时域,频域一
    常用运放选型一览表
    用三段140字符以内的代码生成一张1024×1024的图片
    [收藏夹整理]电子类链接
    [收藏夹整理]三维重构部分
    MSP430之自动增益程控放大main备份
  • 原文地址:https://www.cnblogs.com/sjett/p/IIS.html
Copyright © 2020-2023  润新知