📄 iisadministrator.cs
字号:
// IIsAdmin.NET
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This softwareis provided "AS IS" with no warranties of any kind.
// The entire risk arising out of the use or performance of the software
// and source code is with you.
//
// THIS NOTICE MAY NOT BE REMOVED FROM THIS FILE.
using System;
using System.Collections;
using System.Diagnostics;
using System.DirectoryServices;
using System.IO;
using System.Windows.Forms;
namespace IIsAdmin
{
/// <summary>
/// Defines methods for manipulating IIs programatically
/// </summary>
public class IIsAdministrator : IDisposable
{
private bool _disposed;
private IIsWebSiteCollection _sites;
public event IIsWebSiteEventHandler WebSiteStarted;
public event IIsWebSiteEventHandler WebSiteStopped;
/// <summary>
/// Initializes a new instance of the X class
/// </summary>
public IIsAdministrator()
{
// enumerate the web sites created in IIs
this.RefreshWebSites();
}
#region IDisposable Members
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_sites.Dispose();
}
_disposed = true;
}
}
#endregion
/// <summary>
/// Returns the collection of web sites available in IIs
/// </summary>
public IIsWebSiteCollection WebSites
{
get
{
return _sites;
}
}
/// <summary>
/// Refreshes all of the web sites available in IIs
/// </summary>
public void RefreshWebSites()
{
_sites = IIsAdministrator.GetWebSites();
_sites.WebSiteStarted += new IIsWebSiteEventHandler(OnWebSiteStarted);
_sites.WebSiteStopped += new IIsWebSiteEventHandler(OnWebSiteStopped);
}
/// <summary>
/// Raises the Started event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnWebSiteStarted(object sender, IIsWebSiteEventArgs e)
{
if (this.WebSiteStarted != null)
{
this.WebSiteStarted(sender, e);
}
}
/// <summary>
/// Raises the Stopped event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected virtual void OnWebSiteStopped(object sender, IIsWebSiteEventArgs e)
{
if (this.WebSiteStopped != null)
{
this.WebSiteStopped(sender, e);
}
}
/// <summary>
/// Enumerates the available IIs web sites using DirectoryServices
/// </summary>
/// <returns></returns>
public static IIsWebSiteCollection GetWebSites()
{
// access the root of the directory services for IIs
using (DirectoryEntry root = new DirectoryEntry(IIsConstants.IIsW3SvcPath))
{
IIsWebSiteCollection sites = new IIsWebSiteCollection();
// evaluate each directory entry to see if it is a web site
foreach(DirectoryEntry entry in root.Children)
{
// a web site entry is defined by a SchemaClassName of "IIsWebServer"
if (IsWebServerSchema(entry))
{
// keep the usefull site info
IIsWebSite site = IIsWebSite.FromDirectoryEntry(entry);
sites.Add(site);
}
}
return sites;
}
}
/// <summary>
/// Determines if the DirectoryEntry refers to an IIs web site
/// </summary>
/// <param name="schemaClassName"></param>
/// <returns></returns>
private static bool IsWebServerSchema(DirectoryEntry entry)
{
return (string.Compare(entry.SchemaClassName, IIsConstants.IIsWebServerName, true) == 0);
}
/// <summary>
/// Creates a web site
/// </summary>
/// <param name="name">The name of the site</param>
/// <param name="homeDirectory">The site's home direcotry</param>
/// <param name="port">The web site's port</param>
/// <returns>The newly created site</returns>
public static IIsWebSite CreateWebSite(string name, string homeDirectory, int port)
{
if (name == null || name.Length == 0)
{
throw new ArgumentException("You must specify a web site name");
}
if (homeDirectory == null || homeDirectory.Length == 0)
{
throw new ArgumentException("You must specify a home directory for the web site");
}
if (port < 0)
{
throw new ArgumentException("Your specified port must be greater then or equal to 0");
}
DirectoryEntry root = new DirectoryEntry(IIsConstants.IIsW3SvcPath);
// Finds an open site ID (in IIs 5 it is sequential - in IIs 6 they are random) to try and make it
// work for both, we will just find the largest current one and add one to it
int siteID = 1;
foreach(DirectoryEntry e in root.Children)
{
if(e.SchemaClassName == IIsConstants.IIsWebServerName)
{
int ID = Convert.ToInt32(e.Name);
if(ID > siteID)
{
siteID = ID;
}
}
}
siteID += 1;
// Create the site
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", IIsConstants.IIsWebServerName, siteID);
site.Invoke("Put", "ServerComment", name);
site.Invoke("Put", "KeyType", IIsConstants.IIsWebServerName);
site.Invoke("Put", "ServerBindings", ":" + port.ToString() + ":");
site.Invoke("Put", "ServerState", 1);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", "default.aspx");
site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", 0);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
// Create the application virtual directory
DirectoryEntry siteVDir = site.Children.Add("Root", "IIsWebVirtualDir");
siteVDir.Properties["AppIsolated"][0] = 2;
siteVDir.Properties["Path"][0] = homeDirectory;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["FrontPageWeb"][0] = 1;
siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/" + siteID + "/Root";
siteVDir.Properties["AppFriendlyName"][0] = "Default Application";
siteVDir.CommitChanges();
site.CommitChanges();
// retrieve the entry for the site
DirectoryEntry entry = new DirectoryEntry(IIsConstants.IIsW3SvcPath + "/" + siteID);
// to make the site show up correctly we need to stop and start it, but we also
// want to save the current started site
IIsWebSite newWebSite = IIsWebSite.FromDirectoryEntry(entry);
IIsWebSite currentWebSite = IIsAdministrator.GetWebSites().ActiveWebSite;
if(currentWebSite != null)
{
currentWebSite.Stop();
newWebSite.Start();
newWebSite.Stop();
currentWebSite.Start();
}
else
{
newWebSite.Start();
newWebSite.Stop();
}
// return a new wrapper around the site
return newWebSite;
}
public static void RestartIIs()
{
try
{
string systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System);
string path = Path.Combine(systemPath, IIsConstants.IIsResetPath);
Process p = Process.Start(path);
p.WaitForExit();
}
catch(Exception ex)
{
Debug.WriteLine(ex);
ExceptionUtilities.DisplayException(ex);
}
}
public static void OpenIIsManagementConsole()
{
try
{
string systemPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System);
string path = Path.Combine(systemPath, IIsConstants.IIsMmcConsolePath);
Process.Start(path);
}
catch(Exception ex)
{
Debug.WriteLine(ex);
ExceptionUtilities.DisplayException(ex);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -