WinForm-C#操作IIS_DirectoryEntry

DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");  // IIS://服务器的名字/要操作的Web服务器类型/站点/站点的虚拟目录  

1、创建对象:

 

2、修改对象:

  #region 1、利用DirectoryEntry修改IIS默认站点的端口By网站端口
  DirectoryEntry ent = new DirectoryEntry("IIS://localhost/w3svc");  // 得到现默认站点的IP 端口 描述
  foreach (DirectoryEntry child in ent.Children)
  {
      if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerBindings"].Value != null && child.Properties["ServerBindings"].Value.ToString() == ":8090:")  // 后台系统默认为8090端口
      {
          string strServerBindings = child.Properties["ServerBindings"].Value.ToString();
          //解出端口Port
          char[] splitChar = { ':' };
          string[] strArr = strServerBindings.Split(splitChar);
          string newStrServerBindings = strArr[0] + $":{txtIISConfig_FrontBackPort.Text}:" + strArr[2];

          child.Properties["ServerBindings"].Value = newStrServerBindings;  // 重新给端口赋值
          child.CommitChanges();
      }
  }
  #endregion 利用DirectoryEntry修改IIS默认站点的端口By网站端口

3、删除对象:

 

参考:

    C# 创建虚拟目录

  C#使用DirectoryEntry操作IIS创建网站和虚拟路径

  .Net中如何操作IIS(原理篇)

 

posted @ 2023-04-14 09:28  ꧁执笔小白꧂  阅读(175)  评论(0)    收藏  举报