赞
踩
#region IIS启动/停止
private enum eStates
{
Start = 2,
Stop = 4,
Pause = 6,
}
private string lastWebsite;
private string websiteHash
{
get
{
return string.Format("{0}:{1}:{2}", txtServer.Text, txtUserID.Text, txtPassword.Text);
}
}
private void initWebsiteList()
{
if (websiteHash == lastWebsite)
return;
lastWebsite = websiteHash;
Cursor saveCursor = Cursor;
Cursor = Cursors.WaitCursor;
cmbWebsites.Items.Clear();
cmbWebsites.Items.AddRange(enumerateSites());
if (cmbWebsites.Items.Count > 0)
cmbWebsites.SelectedIndex = 0;
Cursor = saveCursor;
}
/// <summary>
/// 定义开始”或“停止”,当前选择的网站上设置状态
/// </summary>
/// <param name="state"></param>
private void siteInvoke(eStates state)
{
string site = getSiteIdByName(cmbWebsites.SelectedItem.ToString());
if (site == null)
{
// on the odd chance that someone removed the website since we
// enumerated the list
MessageBox.Show("Website '" + cmbWebsites.SelectedItem + "' not found", "Can't " + state + " website");
showStatus(site);
return;
}
lblSite.Text = site;
try
{
ConnectionOptions connectionOptions = new ConnectionOptions();
if (txtUserID.Text.Length > 0)
{
connectionOptions.Username = txtUserID.Text;
connectionOptions.Password = txtPassword.Text;
}
else
{
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
}
ManagementScope managementScope =
new ManagementScope(@"\\" + txtServer.Text + @"\root\microsoftiisv2", connectionOptions);
managementScope.Connect();
if (managementScope.IsConnected == false)
{
MessageBox.Show("Could not connect to WMI namespace " + managementScope.Path, "Connect Failed");
}
else
{
SelectQuery selectQuery =
new SelectQuery("Select * From IIsWebServer Where Name = 'W3SVC/" + site + "'");
using (ManagementObjectSearcher managementObjectSearcher =
new ManagementObjectSearcher(managementScope, selectQuery))
{
foreach (ManagementObject objMgmt in managementObjectSearcher.Get())
objMgmt.InvokeMethod(state.ToString(), new object[0]);
}
}
}
catch (Exception ex)
{
if (ex.ToString().Contains("Invalid namespace"))
{
MessageBox.Show("Invalid Namespace Exception" + Environment.NewLine + Environment.NewLine +
"This program only works with IIS 6 and later", "Can't " + state + " website");
}
else
{
MessageBox.Show(ex.Message, "Can't " + state + " website");
}
}
showStatus(site);
}
/// <summary>
/// 通过siteId找到指定的网站名称
/// </summary>
/// <param name="siteName"></param>
/// <returns></returns>
private string getSiteIdByName(string siteName)
{
DirectoryEntry root = getDirectoryEntry("IIS://" + txtServer.Text + "/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
if (e.Properties["ServerComment"].Value.ToString().Equals(siteName, StringComparison.OrdinalIgnoreCase))
{
return e.Name;
}
}
}
return null;
}
/// <summary>
/// 返回一个字符串数组中可用的网站名称
/// </summary>
/// <returns></returns>
private string[] enumerateSites()
{
List<string> siteNames = new List<string>();
try
{
DirectoryEntry root = getDirectoryEntry("IIS://" + txtServer.Text + "/W3SVC");
foreach (DirectoryEntry e in root.Children)
{
if (e.SchemaClassName == "IIsWebServer")
{
siteNames.Add(e.Properties["ServerComment"].Value.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Can't enumerate websites");
lastWebsite = null;
txtServer.Focus();
txtServer.SelectAll();
}
return siteNames.ToArray();
}
/// <summary>
/// 按名称查找一个网站的显示和更新站点ID和地位
/// </summary>
/// <param name="siteName"></param>
private void findWebsite(string siteName)
{
string site = getSiteIdByName(siteName);
if (site == null)
{
MessageBox.Show("Website '" + siteName + "' not found", "Error");
showStatus(site);
return;
}
lblSite.Text = site;
showStatus(site);
}
/// <summary>
/// 显示指定站点的运行/停止状态ID
/// </summary>
/// <param name="siteId"></param>
private void showStatus(string siteId)
{
string result = "unknown";
DirectoryEntry root = getDirectoryEntry("IIS://" + txtServer.Text + "/W3SVC/" + siteId);
PropertyValueCollection pvc;
pvc = root.Properties["ServerState"];
if (pvc.Value != null)
result = (pvc.Value.Equals((int)eStates.Start) ? "Running" :
pvc.Value.Equals((int)eStates.Stop) ? "Stopped" :
pvc.Value.Equals((int)eStates.Pause) ? "Paused" :
pvc.Value.ToString());
lblStatus.Text = result + " (" + pvc.Value + ")";
}
/// <summary>
/// 返回一个DirectoryEntry对象路径使用可选的用户id和密码
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private DirectoryEntry getDirectoryEntry(string path)
{
if (txtUserID.Text.Length > 0)
return new DirectoryEntry(path, txtUserID.Text, txtPassword.Text);
else
return new DirectoryEntry(path);
}
/// <summary>
/// 设置ID和选择网站的地位
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbWebsites_SelectedIndexChanged_1(object sender, EventArgs e)
{
findWebsite(cmbWebsites.SelectedItem.ToString());
}
private void cmbWebsites_Enter_1(object sender, EventArgs e)
{
initWebsiteList();
}
/// <summary>
/// 启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{
siteInvoke(eStates.Start);
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
siteInvoke(eStates.Stop);
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
#endregion
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。