程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 舉一些C#操作IIS代碼

舉一些C#操作IIS代碼

編輯:C#基礎知識
 using System;
using System.DirectoryServices;
using System.Collections;

namespace OPS.Component
{
    /**//// <summary>
    /// IISWebServer的狀態
    /// </summary>
    public enum IISServerState
    {
        /**//// <summary>
        /// 
        /// </summary>
        Starting = 1,
        /**//// <summary>
        /// 
        /// </summary>
        Started = 2,
        /**//// <summary>
        /// 
        /// </summary>
        Stopping = 3,
        /**//// <summary>
        /// 
        /// </summary>
        Stopped = 4,
        /**//// <summary>
        /// 
        /// </summary>
        Pausing = 5,
        /**//// <summary>
        /// 
        /// </summary>
        Paused = 6,
        /**//// <summary>
        /// 
        /// </summary>
        Continuing = 7
    }
}

 using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using System.DirectoryServices;

namespace OPS.Component
{
    /**//// <summary>
    /// IISWebServer
    /// </summary>
    public class IISWebServer
    {
        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        internal int index = -1;
        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebVirtualDirCollection WebVirtualDirs;
        /**//**/
        /**//// <summary>
        /// 網站說明
        /// </summary>
        public string ServerComment = "Way";
        /**//**/
        /**//// <summary>
        /// 腳本支持
        /// </summary>
        public bool AccessScript = true;
        /**//**/
        /**//// <summary>
        /// 讀取
        /// </summary>
        public bool AccessRead = true;
        /**//**/
        /**//// <summary>
        /// 物理路徑
        /// </summary>
        public string Path = @"c:\";
        /**//**/
        /**//// <summary>
        /// 端口
        /// </summary>
        public int Port = 80;
        /**//**/
        /**//// <summary>
        /// 目錄浏覽
        /// </summary>
        public bool EnableDirBrowsing = false;
        /**//**/
        /**//// <summary>
        /// 默認文檔
        /// </summary>
        public string DefaultDoc = "index.aspx";
        /**//**/
        /**//// <summary>
        /// 使用默認文檔
        /// </summary>
        public bool EnableDefaultDoc = true;

        /**//**/
        /**//// <summary>
        /// IISWebServer的狀態
        /// </summary>
        public IISServerState ServerState
        {
            get
            {
                DirectoryEntry server = IISManagement.returnIISWebserver(this.index);
                if (server == null)
                    throw (new Exception("找不到此IISWebServer"));
                switch (server.Properties["ServerState"][0].ToString())
                {
                    case "2":
                        return IISServerState.Started;
                    case "4":
                        return IISServerState.Stopped;
                    case "6":
                        return IISServerState.Paused;
                }
                return IISServerState.Stopped;
            }
        }

        /**//**/
        /**//// <summary>
        /// 停止IISWebServer
        /// </summary>
        public void Stop()
        {
            DirectoryEntry Server;
            if (index == -1)
                throw (new Exception("在IIS找不到此IISWebServer!"));
            try
            {
                Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
                if (Server != null)
                    Server.Invoke("stop", new object[0]);
                else
                    throw (new Exception("在IIS找不到此IISWebServer!"));
            }
            catch
            {
                throw (new Exception("在IIS找不到此IISWebServer!"));
            }
        }

        /**//**/
        /**//// <summary>
        /// 把基本信息的更改更新到IIS
        /// </summary>
        public void CommitChanges()
        {
            IISManagement.EditIISWebServer(this);
        }

        /**//**/
        /**//// <summary>
        /// 啟動IISWebServer
        /// </summary>
        public void Start()
        {
            if (index == -1)
                throw (new Exception("在IIS找不到此IISWebServer!"));

            DirectoryEntry Service = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC");
            DirectoryEntry Server;
            IEnumerator ie = Service.Children.GetEnumerator();

            while (ie.MoveNext())
            {
                Server = (DirectoryEntry)ie.Current;
                if (Server.SchemaClassName == "IIsWebServer")
                {
                    if (Server.Properties["Serverbindings"][0].ToString() == ":" + this.Port + ":")
                    {
                        Server.Invoke("stop", new object[0]);
                    }
                }
            }

            try
            {
                Server = new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
                if (Server != null)
                    Server.Invoke("start", new object[0]);
                else
                    throw (new Exception("在IIS找不到此IISWebServer!"));
            }
            catch
            {
                throw (new Exception("在IIS找不到此IISWebServer!"));
            }
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebServer()
        {
            WebVirtualDirs = new IISWebVirtualDirCollection(this);
        }
    }
}

 using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component
{
    /**//// <summary>
    /// IISWebServerCollection 
    /// </summary>
    public class IISWebServerCollection : CollectionBase
    {
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebServer this[int Index]
        {
            get
            {
                return (IISWebServer)this.List[Index];

            }
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebServer this[string ServerComment]
        {
            get
            {
                ServerComment = ServerComment.ToLower().Trim();
                IISWebServer list;
                for (int i = 0; i < this.List.Count; i++)
                {
                    list = (IISWebServer)this.List[i];
                    if (list.ServerComment.ToLower().Trim() == ServerComment)
                        return list;
                }
                return null;
            }
        }

        internal void Add_(IISWebServer WebServer)
        {
            this.List.Add(WebServer);
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebServer"></param>
        public void Add(IISWebServer WebServer)
        {
            try
            {
                this.List.Add(WebServer);
                IISManagement.CreateIISWebServer(WebServer);
            }
            catch
            {
                throw (new Exception("發生意外錯誤,可能是某節點將該節點的上級節點作為它自己的子級插入"));
            }

        }

        /**//**/
        /**//// <summary>
        /// 是否包含指定的網站
        /// </summary>
        /// <param name="ServerComment"></param>
        /// <returns></returns>
        public bool Contains(string ServerComment)
        {
            ServerComment = ServerComment.ToLower().Trim();
            for (int i = 0; i < this.List.Count; i++)
            {
                IISWebServer server = this[i];
                if (server.ServerComment.ToLower().Trim() == ServerComment)
                    return true;
            }
            return false;
        }

        /**//**/
        /**//// <summary>
        /// 是否包含指定的網站
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool Contains(int index)
        {
            for (int i = 0; i < this.List.Count; i++)
            {
                IISWebServer server = this[i];
                if (server.index == index)
                    return true;
            }
            return false;
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebServers"></param>
        public void AddRange(IISWebServer[] WebServers)
        {
            for (int i = 0; i <= WebServers.GetUpperBound(0); i++)
            {
                Add(WebServers[i]);
            }
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebServer"></param>
        public void Remove(IISWebServer WebServer)
        {
            for (int i = 0; i < this.List.Count; i++)
            {
                if ((IISWebServer)this.List[i] == WebServer)
                {
                    this.List.RemoveAt(i);
                    return;
                }
            }
            IISManagement.RemoveIISWebServer(WebServer.index);
        }
    }
}

 using System;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component
{
    /**//// <summary>
    /// IISWebVirtualDir
    /// </summary>
    public class IISWebVirtualDir
    {
        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebServer Parent = null;
        /**//**/
        /**//// <summary>
        /// 虛擬目錄名稱
        /// </summary>
        public string Name = "Way";
        /**//**/
        /**//// <summary>
        /// 讀取
        /// </summary>
        public bool AccessRead = true;
        /**//**/
        /**//// <summary>
        /// 腳本支持
        /// </summary>
        public bool AccessScript = true;
        /**//**/
        /**//// <summary>
        /// 物理路徑
        /// </summary>
        public string Path = @"c:\";
        /**//**/
        /**//// <summary>
        /// 默認文檔
        /// </summary>
        public string DefaultDoc = "index.aspx";
        /**//**/
        /**//// <summary>
        /// 使用默認文檔
        /// </summary>
        public bool EnableDefaultDoc = true;
        /**//**/
        /**//// <summary>
        /// 所屬的網站的網站說明
        /// </summary>
        public string WebServer = "";

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebServerName"></param>
        public IISWebVirtualDir(string WebServerName)
        {
            if (WebServerName.ToString() == "")
                throw (new Exception("WebServerName不能為空!"));
            this.WebServer = WebServerName;
        }
        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebVirtualDir()
        {

        }
    }
}

 using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component
{
    /**//// <summary>
    /// IISWebVirtualDirCollection
    /// </summary>
    public class IISWebVirtualDirCollection : CollectionBase
    {
        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebServer Parent = null;

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebVirtualDir this[int Index]
        {
            get
            {
                return (IISWebVirtualDir)this.List[Index];

            }
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        public IISWebVirtualDir this[string Name]
        {
            get
            {
                Name = Name.ToLower();
                IISWebVirtualDir list;
                for (int i = 0; i < this.List.Count; i++)
                {
                    list = (IISWebVirtualDir)this.List[i];
                    if (list.Name.ToLower() == Name)
                        return list;
                }
                return null;
            }
        }


        internal void Add_(IISWebVirtualDir WebVirtualDir)
        {
            try
            {
                this.List.Add(WebVirtualDir);
            }
            catch
            {
                throw (new Exception("發生意外錯誤,可能是某節點將該節點的上級節點作為它自己的子級插入"));
            }

        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebVirtualDir"></param>
        public void Add(IISWebVirtualDir WebVirtualDir)
        {
            WebVirtualDir.Parent = this.Parent;
            try
            {
                this.List.Add(WebVirtualDir);

            }
            catch
            {
                throw (new Exception("發生意外錯誤,可能是某節點將該節點的上級節點作為它自己的子級插入"));
            }
            IISManagement.CreateIISWebVirtualDir(WebVirtualDir, true);

        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebVirtualDirs"></param>
        public void AddRange(IISWebVirtualDir[] WebVirtualDirs)
        {
            for (int i = 0; i <= WebVirtualDirs.GetUpperBound(0); i++)
            {
                Add(WebVirtualDirs[i]);
            }
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="WebVirtualDir"></param>
        public void Remove(IISWebVirtualDir WebVirtualDir)
        {
            for (int i = 0; i < this.List.Count; i++)
            {
                if ((IISWebVirtualDir)this.List[i] == WebVirtualDir)
                {
                    this.List.RemoveAt(i);
                    IISManagement.RemoveIISWebVirtualDir(WebVirtualDir);
                    return;
                }
            }
        }

        /**//**/
        /**//// <summary>
        /// 
        /// </summary>
        /// <param name="Parent"></param>
        public IISWebVirtualDirCollection(IISWebServer Parent)
        {
            this.Parent = Parent;
        }
    }
}

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