}
public class IISManager
{
string strServer = "localhost";
string strWebSiteID = "1";
string strWebSiteName = string.Empty;
string strVirtualPath = string.Empty;
public string Server
{
get { return strServer; }
set { strServer = value; }
}
public string WebSiteID
{
get { return strWebSiteID; }
set { strWebSiteID = value; }
}
public string WebSiteName
{
get { return strWebSiteName; }
set { strWebSiteName = value; }
}
public string VirtualPath
{
get { return strVirtualPath; }
set { strVirtualPath = value; }
}
public IISManager()
{
}
public IISManager(string strS, string strW)
{
strServer = strS;
strWebSiteID = strW;
}
public string GetConfigAllFilePath()
{
DirectoryEntry rootEntry = new DirectoryEntry("IIS://" + strServer + "/w3svc/" + strWebSiteID + "/root");
string strTempPath = string.Empty;
foreach (DirectoryEntry de in rootEntry.Children)
{
if (de.Name == "kintera_com")
{
strTempPath = de.Properties["path"][0].ToString();
break;
}
}
strTempPath = strTempPath.Substring(0, strTempPath.Length - 12);
return strTempPath + @"\CommonLib\includes\INC_config_all.asp";
}
public void CreateWebSite()
{
DirectoryEntry root = new DirectoryEntry("IIS://" + this.Server + "/W3SVC");
if (!EnsureNewSiteAvaible(this.Server))
{
throw(new Exception("The Web Site existed!"));
}
else
{
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", this.WebSiteID);
site.Invoke("Put", "ServerComment", this.WebSiteName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", this.Server);
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", "index.aspx,index.html,index.html,default.aspx,default.htm,default.html");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
}
}
public void CreateVirtualFolder()
{
DirectoryEntry site = new DirectoryEntry("IIS://" + this.Server + "/W3SVC" + this.WebSiteID + "/root");
DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");
siteVDir.Properties["AppIsolated"][0] = 2;
siteVDir.Properties["Path"][0] = this.VirtualPath;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["FrontPageWeb"][0] = 1;
siteVDir.Properties["AppRoot"][0] = "/W3SVC/" + this.WebSiteID + "/Root";
siteVDir.Properties["AppFriendlyName"][0] = "ROOT";
siteVDir.CommitChanges();
site.CommitChanges();
}
public bool EnsureNewSiteAvaible(string bindStr)
{
string strDePath = String.Format("IIS://{0}/w3svc", this.Server);
DirectoryEntry de = new DirectoryEntry(strDePath);
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerBindings"].Value != null)
{
if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
{
return false;
}
}
}
}
return true;
}
public void DeleteWebSiteByName(string siteName)
{
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, siteNum);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
string rootPath = String.Format("IIS://{0}/w3svc", this.Server);
DirectoryEntry rootEntry = new DirectoryEntry(rootPath);
rootEntry.Children.Remove(siteEntry);
rootEntry.CommitChanges();
}
public void DeleteWebSiteByName()
{
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server,this.WebSiteID);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
string rootPath = String.Format("IIS://{0}/w3svc", this.Server);
DirectoryEntry rootEntry = new DirectoryEntry(rootPath);
rootEntry.Children.Remove(siteEntry);
rootEntry.CommitChanges();
}
public void StartWebSite(string siteName)
{
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, siteNum);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
siteEntry.Invoke("Start", new object[] { });
}
public void StartWebSite()
{
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server,this.WebSiteID);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
siteEntry.Invoke("Start", new object[] { });
}
public void StopWebSite(string siteName)
{
string siteNum = GetWebSiteNum(siteName);
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, siteNum);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
siteEntry.Invoke("Stop", new object[] { });
}
public void StopWebSite()
{
string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", this.Server, this.WebSiteID);
DirectoryEntry siteEntry = new DirectoryEntry(siteEntPath);
siteEntry.Invoke("Stop", new object[] { });
}
public string GetWebSiteNum(string siteName)
{
Regex regex = new Regex(siteName);
string tmpStr;
string rootPath = String.Format("IIS://{0}/w3svc", this.Server);
DirectoryEntry deEntry = new DirectoryEntry(rootPath);
foreach (DirectoryEntry child in deEntry.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerBindings"].Value != null)
{
tmpStr = child.Properties["ServerBindings"].Value.ToString();
if (regex.Match(tmpStr).Success)
{
return child.Name;
}
}
if (child.Properties["ServerComment"].Value != null)
{
tmpStr = child.Properties["ServerComment"].Value.ToString();
if (regex.Match(tmpStr).Success)
{
return child.Name;
}
}
}
}
return "No WebSite";
}
public void ResetIIS()
{
Process.Start("iisreset");
}
public void StopIIS()
{
ServiceController sc = new ServiceController("iisadmin");
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
}
//Process.Start("cmd.exe", "/start iisreset");
}
public void StartIIS()
{
ServiceController sc = new ServiceController("iisadmin");
sc.Start();
}
}
public List<string> EnumWebSite()
{
string strDePath = String.Format("IIS://{0}/w3svc", this.Server);
DirectoryEntry de = new DirectoryEntry(strDePath);
List<string> list=new List<string>();
foreach (DirectoryEntry child in de.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
list.Add(child.Properties["ServerComment"].Value.ToString());
}
}
}
// C# 获取网站的 IIS 站点名称 ,获取站点当前连接数 string siteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName(); System.Management.ManagementObject o = new System.Management.ManagementObject("Win32_PerfFormattedData_W3SVC_WebService.Name=siteName"); Response.Write(o.Properties["CurrentConnections"].Value.ToString());<p>服务器IP:<%=Request.ServerVariables["LOCAL_ADDR"]%></p>
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有