当前位置 主页 > 服务器问题 > Linux/apache问题 >

    C#如何控制IIS动态添加删除网站详解

    栏目:Linux/apache问题 时间:2019-11-19 01:05

    我的目的是在Winform程序里面,可以直接启动一个HTTP服务端,给下游客户连接使用。

    查找相关技术,有两种方法:

    1.使用C#动态添加网站应用到IIS中,借用IIS的管理能力来提供HTTP接口。本文即对此做说明

    2.在Winform程序中实现Web服务器逻辑,自己监听和管理客户端请求;

    利用IIS7自带类库管理IIS现在变的更强大更方便,而完全可以不需要用DirecotryEntry这个类了(乐博网中很多.net管理iis6.0的文章都用到了DirecotryEntry这个类 ),Microsoft.Web.Administration.dll位于IIS的目录(%WinDir%\\System32\\InetSrv)下,使用时需要引用,它基本上可以管理IIS7的各项配置。

    这个类库的主体结构如下:

     

    这里只举几个例子说明一下基本功能,更多功能请参考MSDN。

    建立站点

    string SiteName="乐博网"; //站点名称
    string BindArgs="*:80:"; //绑定参数,注意格式
    string apl="http"; //类型
    string path="e:\\乐博网"; //网站路径
    ServerManager sm = new ServerManager();
    sm.Sites.Add(SiteName,apl,BindArgs,path);
    sm.CommitChanges();

    修改站点

    Site site=sm.Sites["newsite"];
    site.Name=SiteName;
    site.Bindings[0].EndPoint.Port=9999;
    site.Applications[0].VirtualDirectories[0].PhysicalPath=path;
    sm.CommitChanges();

    删除站点

    Site site=sm.Sites["乐博网"];
    sm.Sites.Remove(site);
    sm.CommitChanges();

    站点操作

    方法一:

    #region CreateWebsite 添加网站
    
      public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port)
      {
       try
       {
        ManagementObject oW3SVC = new ManagementObject (_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
        if (IsWebSiteExists (serverID))
        {
         return "Site Already Exists...";
        }
    
        ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters ("CreateNewSite");
        ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];
    
        serverBinding[0] = CreateServerBinding(HostName, IP, Port);
    
        inputParameters["ServerComment"] = serverComment;
        inputParameters["ServerBindings"] = serverBinding;
        inputParameters["PathOfRootVirtualDir"] = defaultVrootPath;
        inputParameters["ServerId"] = serverID;
    
        ManagementBaseObject outParameter = null;
        outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
    
        // 启动网站
        string serverName = "W3SVC/" + serverID;
        ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
        webSite.InvokeMethod("Start", null);
    
        return (string)outParameter.Properties["ReturnValue"].Value;
    
       }
       catch (Exception ex)
       {
        return ex.Message;
       }
    
      }
    
      public ManagementObject CreateServerBinding(string HostName, string IP, string Port)
      {
       try
       {
        ManagementClass classBinding = new ManagementClass(_scope, new ManagementPath("ServerBinding"), null);
    
        ManagementObject serverBinding = classBinding.CreateInstance();
    
        serverBinding.Properties["Hostname"].Value = HostName;
        serverBinding.Properties["IP"].Value = IP;
        serverBinding.Properties["Port"].Value = Port;
        serverBinding.Put();
    
        return serverBinding;
       }
       catch
       {
        return null;
       }
      }
      
      #endregion
    
      #region 添加网站事件
    
      protected void AddWebsite_Click(object sender, EventArgs e)
      {
       IISManager iis = new IISManager();
    
       iis.Connect();
    
       string serverID = "5556";
       string serverComment = "Create Website";
       string defaultVrootPath = @"D:\web";
       string HostName = "World";
       string IP = "";
       string Port = "9898";
    
       ReturnMessage.Text = iis.CreateWebSite(serverID,serverComment,defaultVrootPath,HostName,IP,Port);
      }
    
      #endregion
    
      #region DeleteSite 删除站点
    
      public string DeleteSite(string serverID)
      {
       try
       {
        string serverName = "W3SVC/" + serverID;
        ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
        webSite.InvokeMethod("Stop", null);
        webSite.Delete();
        webSite = null;
    
        return "Delete the site succesfully!";
       }
       catch (Exception deleteEx)
       {
        return deleteEx.Message;
       }
      }
    
      #endregion