分享

C#操作IIS的代码 - 双赢、诚信 - 博客园

 webgj 2009-03-16

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

    }

}


IISWebServerCollection


IISWebVirtualDir

IISWebVirtualDirCollection

IISManagement

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多