当前位置:web 服务器 > IIS >>

用C#操作IIS创建虚拟目录和网站

#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

 

页面:

// 添加网站

    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);

    }

 

删除网站的代码:

#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

 

补充:软件开发 , C# ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
访问www.zzzyk.com 试试
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,