举一些C#操作IIS代码

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

    }

}

 
 
posted @ 2014-05-24 12:13  zzg168  阅读(239)  评论(0)    收藏  举报