?? iisutil.cs
字號:
using System;
using System.Collections;
using System.Data;
using System.DirectoryServices;
namespace Vml.Utilities
{
/// <summary>
/// Summary description for IISUtil.
/// </summary>
public class IISUtil
{
private const string WEB_SERVER_KEY = "IIsWebServer";
private const string IIS_ENTRY = "IIS://localhost/W3SVC";
private IISUtil(){}
public static int CreateWebSite(string webSiteName, string pathToRoot, int port)
{
// Funcation argument error handling
if(webSiteName == null || webSiteName.Length == 0)
{
throw new ArgumentException("You must specify a web site name");
}
if(pathToRoot == null || pathToRoot.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(IIS_ENTRY);
// 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 == WEB_SERVER_KEY)
{
int ID = Convert.ToInt32(e.Name);
if(ID > siteID)
{
siteID = ID;
}
}
}
siteID += 1;
// Create the site
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", WEB_SERVER_KEY, siteID);
site.Invoke("Put", "ServerComment", webSiteName);
site.Invoke("Put", "KeyType", WEB_SERVER_KEY);
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] = pathToRoot;
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();
return siteID;
}
public static void StopWebSite(int siteID)
{
DirectoryEntry root = new DirectoryEntry(IIS_ENTRY + "/" + siteID);
root.Invoke("stop");
}
public static void StartWebSite(int siteID)
{
DirectoryEntry root = new DirectoryEntry(IIS_ENTRY + "/" + siteID);
root.Invoke("start");
}
public static void StopAllWebSites()
{
DirectoryEntry root = new DirectoryEntry(IIS_ENTRY);
foreach(DirectoryEntry e in root.Children)
{
if(e.SchemaClassName == WEB_SERVER_KEY)
{
e.Invoke("stop");
}
}
}
public static int GetStartedSiteID()
{
DirectoryEntry root = new DirectoryEntry(IIS_ENTRY);
int siteID = -1;
foreach(DirectoryEntry e in root.Children)
{
if(e.SchemaClassName == WEB_SERVER_KEY)
{
if(Convert.ToInt32(e.Invoke("Get", "ServerState")) == 2)
{
siteID = Convert.ToInt32(e.Name);
}
}
}
return siteID;
}
public static void DeleteWebSite(int siteID)
{
DirectoryEntry root = new DirectoryEntry(IIS_ENTRY);
root.Invoke("Delete", WEB_SERVER_KEY, siteID);
}
public static DataTable GetWebSites()
{
DirectoryEntry root = new DirectoryEntry(IIS_ENTRY);
DataTable dt = new DataTable("Sites");
dt.Columns.Add(new DataColumn("ID", typeof(string)));
dt.Columns.Add(new DataColumn("SiteName", typeof(string)));
dt.Columns.Add(new DataColumn("State", typeof(string)));
foreach(DirectoryEntry e in root.Children)
{
if(e.SchemaClassName == WEB_SERVER_KEY)
{
DataRow dr = dt.NewRow();
dr["ID"] = e.Name;
dr["SiteName"] = Convert.ToString(e.Invoke("Get", "ServerComment"));
dr["State"] = GetUserFriendlyState(Convert.ToInt32(e.Invoke("Get", "ServerState")));
dt.Rows.Add(dr);
}
}
return dt;
}
private static string GetUserFriendlyState(int currentState)
{
switch(currentState)
{
case 1:
return "Starting";
case 2:
return "Started";
case 3:
return "Stopping";
case 4:
return "Stopped";
case 5:
return "Pausing";
case 6:
return "Paused";
case 7:
return "Continuing";
default:
return "Unknown";
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -