用 ASP.NET 管理 IIS(转)

it2022-05-09  25

在 ASP.NET(2.0 及以上版本)中,我们可以使用 System.DirectoryServices 来管理 IIS。

System.DirectoryServices 可以获取和设置 IIS metabase 的 String and DWORD 属性,并且也能调用大多数方法。

前提是您的操作系统应该是 Windows XP Professional 带 SP2,或 Windows Server 2003 带 SP1 或更新版本的操作系统。否则您可能会得到类似“The directory cannot report the number of properties”的错误。

不过开始之前,您应该在网站解决方案的属性页上 References 中引入 System.DirectoryServices。添加后,web.config 中应该有类似如下的内容:

<configuration> <system.web> <compilation debug="false"> <assemblies> <add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

另外还请您注意一下,您运行 ASP.NET 的帐户要有足够的权限来操作 IIS。

关于用 System.DirectoryServices 来管理 IIS 的所有内容请参见 MSDN 上的 http://msdn.microsoft.com/en-us/library/ms525791.aspx

另外我摘录了一点示例,是关于如何创建站点、设置端口、创建虚拟目录,虽然是 Console 环境中的,但只需要作些很简单的修改,就可以应用到 ASP.NET 中。

using System; using System.IO; using System.DirectoryServices; using System.Reflection; using System.Runtime.InteropServices; using System.Collections; namespace System_DirectoryServices_DirectoryEntry_ConfigIIS { class Program { static void Main( string[] args) { ... CreateSite( "IIS://Localhost/W3SVC", "555", "MySite", "D:\\Inetpub\\Wwwroot"); ... SetSingleProperty( "IIS://Localhost/W3SVC/555", "ServerBindings", ":8080:"); ... CreateVDir( "IIS://Localhost/W3SVC/1/Root", "MyVDir", "D:\\Inetpub\\Wwwroot"); ... } ... static void CreateSite( string metabasePath, string siteID, string siteName, string physicalPath) { //  metabasePath is of the form "IIS://<servername>/<service>" //    for example "IIS://localhost/W3SVC" //  siteID is of the form "<number>", for example "555" //  siteName is of the form "<name>", for example, "My New Site" //  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot" Console.WriteLine( "\nCreating site {0}/{1}, mapping the Root application to {2}:", metabasePath, siteID, physicalPath); try { DirectoryEntry service = new DirectoryEntry(metabasePath); string className = service.SchemaClassName.ToString(); if (className.EndsWith( "Service")) { DirectoryEntries sites = service.Children; DirectoryEntry newSite = sites.Add(siteID, (className.Replace( "Service", "Server"))); newSite.Properties[ "ServerComment"][0] = siteName; newSite.CommitChanges(); DirectoryEntry newRoot; newRoot = newSite.Children.Add( "Root", "IIsWebVirtualDir"); newRoot.Properties[ "Path"][0] = physicalPath; newRoot.Properties[ "AccessScript"][0] = true; newRoot.CommitChanges(); Console.WriteLine( " Done. Your site will not start until you set the ServerBindings or SecureBindings property."); } else Console.WriteLine( " Failed. A site can only be created in a service node."); } catch (Exception ex) { Console.WriteLine( "Failed in CreateSite with the following exception: \n{0}", ex.Message); } } ... static void SetSingleProperty( string metabasePath, string propertyName, object newValue) { //  metabasePath is of the form "IIS://<servername>/<path>" //    for example "IIS://localhost/W3SVC/1" //  propertyName is of the form "<propertyName>", for example "ServerBindings" //  value is of the form "<intStringOrBool>", for example, ":80:" Console.WriteLine( "\nSetting single property at {0}/{1} to {2} ({3}):", metabasePath, propertyName, newValue, newValue.GetType().ToString()); try { DirectoryEntry path = new DirectoryEntry(metabasePath); PropertyValueCollection propValues = path.Properties[propertyName]; string oldType = propValues.Value.GetType().ToString(); string newType = newValue.GetType().ToString(); Console.WriteLine( " Old value of {0} is {1} ({2})", propertyName, propValues.Value, oldType); if (newType == oldType) { path.Properties[propertyName][0] = newValue; path.CommitChanges(); Console.WriteLine( "Done"); } else Console.WriteLine( " Failed in SetSingleProperty; type of new value does not match property"); } catch (Exception ex) { if ( "HRESULT 0x80005006" == ex.Message) Console.WriteLine( " Property {0} does not exist at {1}", propertyName, metabasePath); else Console.WriteLine( "Failed in SetSingleProperty with the following exception: \n{0}", ex.Message); } } ... static void CreateVDir( string metabasePath, string vDirName, string physicalPath) { //  metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]" //    for example "IIS://localhost/W3SVC/1/Root" //  vDirName is of the form "<name>", for example, "MyNewVDir" //  physicalPath is of the form "<drive>:\<path>", for example, "C:\Inetpub\Wwwroot" Console.WriteLine( "\nCreating virtual directory {0}/{1}, mapping the Root application to {2}:", metabasePath, vDirName, physicalPath); try { DirectoryEntry site = new DirectoryEntry(metabasePath); string className = site.SchemaClassName.ToString(); if ((className.EndsWith( "Server")) || (className.EndsWith( "VirtualDir"))) { DirectoryEntries vdirs = site.Children; DirectoryEntry newVDir = vdirs.Add(vDirName, (className.Replace( "Service", "VirtualDir"))); newVDir.Properties[ "Path"][0] = physicalPath; newVDir.Properties[ "AccessScript"][0] = true; // These properties are necessary for an application to be created. newVDir.Properties[ "AppFriendlyName"][0] = vDirName; newVDir.Properties[ "AppIsolated"][0] = "1"; newVDir.Properties[ "AppRoot"][0] = "/LM" + metabasePath.Substring(metabasePath.IndexOf( "/", ( "IIS://".Length))); newVDir.CommitChanges(); Console.WriteLine( " Done."); } else Console.WriteLine( " Failed. A virtual directory can only be created in a site or virtual directory node."); } catch (Exception ex) { Console.WriteLine( "Failed in CreateVDir with the following exception: \n{0}", ex.Message); } } ... } }

转载于:https://www.cnblogs.com/n666/archive/2009/12/15/2190979.html

相关资源:替代IIS运行asp.net程序,布署.net程序就太方便了

最新回复(0)