C# 操作IIS -App & AppPools

时间:2023-03-08 17:03:14
C# 操作IIS -App & AppPools
 using System;
using System.DirectoryServices;
using System.Management;
using Microsoft.Web.Administration; namespace ConsoleApplication_IsNullTest
{
/// <summary>
/// IIS Helper.
/// </summary>
public class IISHelper
{ private static string siteName = "siteName";
private static string sitePath = "sitePath";
private static string serverId = "";
private static string runtimeVersion="v4.0"; public static string RuntimeVersion
{
get { return runtimeVersion; }
} public static DirectoryEntry iisSite = new DirectoryEntry("IIS://localhost/W3SVC", "UserName", "Userpwd"); private static ManagementScope scope = null; public IISHelper()
{
scope = new ManagementScope("ConfigManager.ManagementScopePath");
if (scope.IsConnected == false)
{
scope.Connect();
}
} /// <summary>
/// 创建应用程序池
/// </summary>
/// <param name="AppPoolName">程序池名</param>
/// <param name="PipelineModel">Managed pipeline model.</param>
public static void CreateAppPool(string AppPoolName, ManagedPipelineMode pipelineMode)
{
DirectoryEntry newpool;
DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");
newpool.Properties["ManagedRuntimeVersion"].Value = runtimeVersion;
newpool.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated;
newpool.CommitChanges();
} /// <summary>
/// 创建site
/// </summary>
public static void CreateWebSite(Object site)
{
ManagementObject manage = new ManagementObject(scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
ManagementBaseObject inputParameters = manage.GetMethodParameters("CreateNewSite");
ManagementBaseObject[] serverBinding = new ManagementBaseObject[];
serverBinding[] = CreateServerBinding("", "127.0.0.1", "");
inputParameters["ServerComment"] = siteName;
inputParameters["ServerBindings"] = serverBinding;
inputParameters["PathOfRootVirtualDir"] = sitePath;
inputParameters["ServerId"] = serverId;
try
{
manage.InvokeMethod("CreateNewSite", inputParameters, null);
}
catch
{
throw new Exception("createNewSite");
}
SetAppToPool(siteName, serverId); System.Threading.Thread.Sleep(); //启动web site
string serverName = "W3SVC/" + serverId;
ManagementObject manageSite = new ManagementObject(scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
manageSite.InvokeMethod("Start", null);
} /// <summary>
/// 关联应用程序池
/// </summary>
/// <param name="appPoolName"></param>
/// <param name="serverId"></param>
private static void SetAppToPool(string appPoolName, string serverId)
{
string path = string.Format("IIS://localhost/W3SVC/{0}/Root", serverId);
DirectoryEntry entry = new DirectoryEntry(path);
entry.Properties["AppPoolId"].Value = appPoolName;
entry.CommitChanges();
entry.Close();
} /// <summary>
/// Get site url前缀
/// </summary>
/// <param name="HostName"></param>
/// <param name="IP"></param>
/// <param name="Port"></param>
/// <returns></returns>
private static ManagementObject CreateServerBinding(string HostName, string IP, string Port)
{
try
{
ManagementClass classBinding = new ManagementClass(scope, new ManagementPath("ServerBinding"), null);
ManagementObject serverBinding = classBinding.CreateInstance();
serverBinding.Properties["Hostname"].Value = HostName;
serverBinding.Properties["IP"].Value = IP;
serverBinding.Properties["Port"].Value = Port;
serverBinding.Put();
return serverBinding;
}
catch
{
return null;
}
} /// <summary>
/// 创建web site 子项的site $ 应用程序池
/// </summary>
/// <param name="site"></param>
/// <param name="serviceName"></param>
/// <param name="AppolName"></param>
public static void CreateApplication(Object site, string serviceName, string AppolName)
{
DirectoryEntry defaultWebSite = GetWebisteDirectory("FUSiteName");//父级site name
if (defaultWebSite != null)
{
DirectoryEntry defaultWebSiteRoot = new DirectoryEntry(defaultWebSite.Path + "/Root"); //Create and setup new virtual directory
DirectoryEntry virtualDirectory = defaultWebSiteRoot.Children.Add(siteName, "IIsWebVirtualDir"); virtualDirectory.Properties["Path"][] = sitePath;
virtualDirectory.Properties["AppFriendlyName"][] = siteName;
virtualDirectory.CommitChanges(); virtualDirectory.Invoke("AppCreate", );
object[] param = { , siteName, true };
virtualDirectory.Invoke("AppCreate3", param);
virtualDirectory.Properties["AppPoolId"].Value = AppolName; string appPoolPath = @"IIS://localhost/W3SVC/AppPools/" + AppolName;
try
{
var appPoolEntry = new DirectoryEntry(appPoolPath);
appPoolEntry.Properties["ManagedRuntimeVersion"].Value = runtimeVersion;
appPoolEntry.Properties["ManagedPipelineMode"].Value = ManagedPipelineMode.Integrated;
appPoolEntry.Invoke("SetInfo", null);
appPoolEntry.CommitChanges();
appPoolEntry.Close();
}
catch
{ }
virtualDirectory.CommitChanges();
virtualDirectory.Close();
}
} /// <summary>
/// Get web site directory.
/// </summary>
/// <param name="websiteName"></param>
/// <returns></returns>
private static DirectoryEntry GetWebisteDirectory(string websiteName)
{
DirectoryEntries des = iisSite.Children;
foreach (DirectoryEntry sub in des)
{
if (sub.SchemaClassName == "IIsWebServer" && sub.Properties["ServerComment"].Contains(websiteName.Trim()))
{ return sub;
}
}
return null;
}
}
}