程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 用C#操作IIS創建虛擬目錄和網站

用C#操作IIS創建虛擬目錄和網站

編輯:C#入門知識

#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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved