C#操作IIS代码

ContractedBlock.gif ExpandedBlockStart.gif IISServerState
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
    }
}


ContractedBlock.gif ExpandedBlockStart.gif IISWebServer
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using System.DirectoryServices;

namespace OPS.Component
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// IISWebServer
    
/// </summary>

    public class IISWebServer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        internal int index = -1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebVirtualDirCollection WebVirtualDirs;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 网站说明
        
/// </summary>

        public string ServerComment = "Way";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 脚本支持
        
/// </summary>

        public bool AccessScript = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 读取
        
/// </summary>

        public bool AccessRead = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 物理路径
        
/// </summary>

        public string Path = @"c:\";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 端口
        
/// </summary>

        public int Port = 80;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 目录浏览
        
/// </summary>

        public bool EnableDirBrowsing = false;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 默认文档
        
/// </summary>

        public string DefaultDoc = "index.aspx";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 使用默认文档
        
/// </summary>

        public bool EnableDefaultDoc = true;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// IISWebServer的状态
        
/// </summary>

        public IISServerState ServerState
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                DirectoryEntry server 
= IISManagement.returnIISWebserver(this.index);
                
if (server == null)
                    
throw (new Exception("找不到此IISWebServer"));
                
switch (server.Properties["ServerState"][0].ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
case "2":
                        
return IISServerState.Started;
                    
case "4":
                        
return IISServerState.Stopped;
                    
case "6":
                        
return IISServerState.Paused;
                }

                
return IISServerState.Stopped;
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 停止IISWebServer
        
/// </summary>

        public void Stop()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            DirectoryEntry Server;
            
if (index == -1)
                
throw (new Exception("在IIS找不到此IISWebServer!"));
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Server 
= new DirectoryEntry("IIS://" + IISManagement.Machinename + "/W3SVC/" + index);
                
if (Server != null)
                    Server.Invoke(
"stop"new object[0]);
                
else
                    
throw (new Exception("在IIS找不到此IISWebServer!"));
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw (new Exception("在IIS找不到此IISWebServer!"));
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 把基本信息的更改更新到IIS
        
/// </summary>

        public void CommitChanges()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            IISManagement.EditIISWebServer(
this);
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 启动IISWebServer
        
/// </summary>

        public void Start()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
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())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Server 
= (DirectoryEntry)ie.Current;
                
if (Server.SchemaClassName == "IIsWebServer")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (Server.Properties["Serverbindings"][0].ToString() == ":" + this.Port + ":")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        Server.Invoke(
"stop"new object[0]);
                    }

                }

            }


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

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw (new Exception("在IIS找不到此IISWebServer!"));
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebServer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            WebVirtualDirs 
= new IISWebVirtualDirCollection(this);
        }

    }

}


ContractedBlock.gif ExpandedBlockStart.gif IISWebServerCollection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// IISWebServerCollection 
    
/// </summary>

    public class IISWebServerCollection : CollectionBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebServer this[int Index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (IISWebServer)this.List[Index];

            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebServer this[string ServerComment]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ServerComment 
= ServerComment.ToLower().Trim();
                IISWebServer list;
                
for (int i = 0; i < this.List.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    list 
= (IISWebServer)this.List[i];
                    
if (list.ServerComment.ToLower().Trim() == ServerComment)
                        
return list;
                }

                
return null;
            }

        }


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


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebServer"></param>

        public void Add(IISWebServer WebServer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.List.Add(WebServer);
                IISManagement.CreateIISWebServer(WebServer);
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw (new Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"));
            }


        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 是否包含指定的网站
        
/// </summary>
        
/// <param name="ServerComment"></param>
        
/// <returns></returns>

        public bool Contains(string ServerComment)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ServerComment 
= ServerComment.ToLower().Trim();
            
for (int i = 0; i < this.List.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IISWebServer server 
= this[i];
                
if (server.ServerComment.ToLower().Trim() == ServerComment)
                    
return true;
            }

            
return false;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 是否包含指定的网站
        
/// </summary>
        
/// <param name="index"></param>
        
/// <returns></returns>

        public bool Contains(int index)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < this.List.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IISWebServer server 
= this[i];
                
if (server.index == index)
                    
return true;
            }

            
return false;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebServers"></param>

        public void AddRange(IISWebServer[] WebServers)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i <= WebServers.GetUpperBound(0); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Add(WebServers[i]);
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebServer"></param>

        public void Remove(IISWebServer WebServer)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < this.List.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if ((IISWebServer)this.List[i] == WebServer)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.List.RemoveAt(i);
                    
return;
                }

            }

            IISManagement.RemoveIISWebServer(WebServer.index);
        }

    }

}



ContractedBlock.gif ExpandedBlockStart.gif IISWebVirtualDir
using System;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// IISWebVirtualDir
    
/// </summary>

    public class IISWebVirtualDir
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebServer Parent = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 虚拟目录名称
        
/// </summary>

        public string Name = "Way";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 读取
        
/// </summary>

        public bool AccessRead = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 脚本支持
        
/// </summary>

        public bool AccessScript = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 物理路径
        
/// </summary>

        public string Path = @"c:\";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 默认文档
        
/// </summary>

        public string DefaultDoc = "index.aspx";
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 使用默认文档
        
/// </summary>

        public bool EnableDefaultDoc = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 所属的网站的网站说明
        
/// </summary>

        public string WebServer = "";

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebServerName"></param>

        public IISWebVirtualDir(string WebServerName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
if (WebServerName.ToString() == "")
                
throw (new Exception("WebServerName不能为空!"));
            
this.WebServer = WebServerName;
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebVirtualDir()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{

        }

    }

}


ContractedBlock.gif ExpandedBlockStart.gif IISWebVirtualDirCollection
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace OPS.Component
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// IISWebVirtualDirCollection
    
/// </summary>

    public class IISWebVirtualDirCollection : CollectionBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebServer Parent = null;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebVirtualDir this[int Index]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return (IISWebVirtualDir)this.List[Index];

            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>

        public IISWebVirtualDir this[string Name]
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Name 
= Name.ToLower();
                IISWebVirtualDir list;
                
for (int i = 0; i < this.List.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    list 
= (IISWebVirtualDir)this.List[i];
                    
if (list.Name.ToLower() == Name)
                        
return list;
                }

                
return null;
            }

        }



        
internal void Add_(IISWebVirtualDir WebVirtualDir)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.List.Add(WebVirtualDir);
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw (new Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"));
            }


        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebVirtualDir"></param>

        public void Add(IISWebVirtualDir WebVirtualDir)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            WebVirtualDir.Parent 
= this.Parent;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.List.Add(WebVirtualDir);

            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
throw (new Exception("发生意外错误,可能是某节点将该节点的上级节点作为它自己的子级插入"));
            }

            IISManagement.CreateIISWebVirtualDir(WebVirtualDir, 
true);

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebVirtualDirs"></param>

        public void AddRange(IISWebVirtualDir[] WebVirtualDirs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i <= WebVirtualDirs.GetUpperBound(0); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Add(WebVirtualDirs[i]);
            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="WebVirtualDir"></param>

        public void Remove(IISWebVirtualDir WebVirtualDir)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < this.List.Count; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if ((IISWebVirtualDir)this.List[i] == WebVirtualDir)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.List.RemoveAt(i);
                    IISManagement.RemoveIISWebVirtualDir(WebVirtualDir);
                    
return;
                }

            }

        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**/
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 
        
/// </summary>
        
/// <param name="Parent"></param>

        public IISWebVirtualDirCollection(IISWebServer Parent)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.Parent = Parent;
        }

    }

}


ContractedBlock.gif ExpandedBlockStart.gif IISManagement

转载于:https://www.cnblogs.com/StarYe/archive/2009/08/12/1544114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值