using System;
using System.Net;
using Microsoft.Win32;
using System.ServiceProcess;
using System.DirectoryServices;
using System.ComponentModel;
namespace CommonLogic
{
public class IISHelper
{
public static readonly string SERVICENAME = "W3SVC";
public IISHelper()
{
}
/// <summary>
/// Get the root of the IIS from the registry.
/// e.g. C:\Inetpub\wwwroot.
/// </summary>
/// <returns></returns>
public static string GetIISRoot()
{
try
{
RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\InetStp", false);
return registry.GetValue("PathWWWRoot").ToString();
}
catch
{
return "";
}
}
public static bool IsStart()
{
return IsStart(GetMachineName());
}
/// <summary>
/// Check whether the w3svc service start or not.
/// </summary>
/// <returns>bool result.</returns>
public static bool IsStart(string machineName)
{
ServiceController controller = new ServiceController(SERVICENAME, machineName);
if (controller.Status.ToString().ToLower().Contains("run"))
{
return true;
}
else
{
return false;
}
}
public static string GetMachineName()
{
return Dns.GetHostName();
}
public static void Start()
{
Start(GetMachineName());
}
public static void Start(string machineName)
{
ServiceController controller = new ServiceController(SERVICENAME, machineName);
controller.Start();
}
public static void Test()
{
string entPath = String.Format("IIS://{0}/w3svc/1/Root", "Localhost"); // your computer name.
DirectoryEntry ent = GetDirectoryEntry(entPath);
foreach (DirectoryEntry child in ent.Children)
{
//if (child.SchemaClassName == "IIsWebServer")
//{
// if (child.Properties["ServerBindings"].Value != null)
// {
// if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
// {
// return false;
// }
// }
//}
if (child.Name == "TCMService")
{
Console.WriteLine(child.Path);
//child.Properties[
}
//Console.WriteLine(child.Name);
//Console.WriteLine(child.SchemaClassName);
//Console.WriteLine(child.Properties["ServerBindings"].Value.ToString());
}
}
public static DirectoryEntry GetDirectoryEntry(string entPath)
{
DirectoryEntry ent;
//if (UserName == null)
//{
ent = new DirectoryEntry(entPath);
//}
//else
//{
// // ent = new DirectoryEntry(entPath, HostName+"\\"+UserName, Password, AuthenticationTypes.Secure);
// ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);
//}