zoukankan      html  css  js  c++  java
  • Get IIS Application Id & name

    In this case it’s using IIS 7 so the .NET version isn’t selectable as it’s selected as part of the Application
    Pool. For IIS 6 both .NET version and App Pool are available for IIS 5 only the .NET version is available.

    So how do you get the ApplicationPools available, select and set one and create a new one? There are actually a number of ways (especially with IIS7) but the most widely supported by recent versions of IIS is still by using ADSI and DirectoryServices in

    .NET.

     

    I talked about using ADSI for IIS with .NET a long while back with some examples of how to get virtuals and set properties etc. there.

    So, here’s some additional code to deal with Application Pools in my WebConfiguration class (note there are a few depencies in this code, but you should be able to glean the general

    idea):

     

    /// <summary>

    /// Returns a list of all

    the Application Pools configured

    /// </summary>

    /// <returns></returns>

    public ApplicationPool[] GetApplicationPools()

    {           

        if (ServerType != WebServerTypes.IIS6 &&

    ServerType != WebServerTypes.IIS7)

    return null;

     

        DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName

    + "/W3SVC/AppPools");


        if (root == null)         

    return null;

        List<ApplicationPool> Pools = new List<ApplicationPool>();

        foreach (DirectoryEntry Entry in root.Children)

        {

      PropertyCollection Properties = Entry.Properties;     

    ApplicationPool Pool = new ApplicationPool();    

    Pool.Name = Entry.Name;        
    Pools.Add(Pool);

        }

        return Pools.ToArray();

    }

    ///<summary>

    /// Create a new Application Pool and return an instance of the entry

    /// </summary>

    /// <param name="AppPoolName"></param>

    /// <returns></returns>

    public DirectoryEntry CreateApplicationPool(string AppPoolName)

    {

        if (this.ServerType != WebServerTypes.IIS6 &&     

    this.ServerType != WebServerTypes.IIS7)     

    return null;


        DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName
    + "/W3SVC/AppPools");

        if (root == null)

    return null;

        DirectoryEntry AppPool = root.Invoke("Create","IIsApplicationPool",AppPoolName) as DirectoryEntry;               

    AppPool.CommitChanges(); 

      return AppPool;

    }

     


    /// <summary>

    /// Returns an instance of an Application Pool
    /// </summary>

    /// <param name="AppPoolName"></param>

    /// <returns></returns>

    public DirectoryEntry GetApplicationPool(string AppPoolName)

    {

        DirectoryEntry root = this.GetDirectoryEntry("IIS://" + this.DomainName + "/W3SVC/AppPools/" + AppPoolName);

        return root;
    }

     

    /// <summary>

    /// Retrieves an Adsi Node by its path. Abstracted for error handling/// </summary>

    /// <param name="Path">the ADSI path to retrieve: IIS://localhost/w3svc/root</param>

    /// <returns>node or null</returns>

    private DirectoryEntry GetDirectoryEntry(string Path)

    {

         DirectoryEntry root = null;

        try

        {

       root = new DirectoryEntry(Path);

        }

        catch

        {

      this.SetError("Couldn't access node"); return null;

        }


        if (root == null)

        {

    this.SetError("Couldn't access node");

    return null;

        }

        return root;

    }

     

    AppPools are stored under:

     

    IIS://localhost/W3SVC/AppPools

    And you can access a specific pool through the Children collection. For ADSI paths a child looks like this:

    IIS://localhost/W3SVC/AppPools/DefaultAppPool

    From there you get a DirectoryEntry object and you can fire away on the properties of the pool and set thing like the impersonating account and various health checks. I didn’t need to
    look closely at this but I couldn’t find the MSDN documentation on IIsApplicationPool that shows the member properties. The MSDN documentation for IIS’s ADSI support (and even the IIS6 WMI support) is
    just plain awful and scattered through many places. If anybody happens to find a link with the member properties post it here.


    Once you have an Application Pool it can be attached to a virtual directory/Application via its AppPoolId:

     DirectoryEntry VDir = new DirectoryEntry("IIS://localhost/W3SVC/ROOT/WebStore");
    VDir.Properties["AppPoolId"].Value = this.ApplicationPool;

    VDir.CommitChanges();

     
    And there you have it. I’ve updated the Web Configuration Utility online and it includes the AppPool configuration code, and a recent update to the West Wind Web Store also includes this code as part of the configuration.

     



    using System.DirectoryServices;
    using System;

    public class IISAdmin
    {
       public static void GetWebsiteID(string websiteName)
       {
          DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");

         foreach(DirectoryEntry de in w3svc.Children)
         {
            if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
            {
               Console.Write(de.Name);
            }

         }

      }
      public static void Main()
      {
         GetWebsiteID("Default Web Site");
      }


    -----------------------------------------------------------

    You are looking for ServerManager
    (Microsoft.Web.Administration) which provides read and write
    access to the IIS 7.0 configuration system.



    Iterate through Microsoft.Web.Administration.SiteCollection, get a
    reference to your website using the Site Object and read the value of
    the Name property.



    // Snippet        
    using (ServerManager serverManager = new ServerManager()) {

    var sites = serverManager.Sites;
    foreach (Site site in sites) {
             Console.WriteLine(site.Name); // This will return the WebSite name
    }


    You can also use LINQ to query the ServerManager.Sites collection
    (see example below)



    // Start all stopped WebSites using the power of Linq :)
    var sites = (from site in serverManager.Sites
                where site.State == ObjectState.Stopped
                orderby site.Name
                select site);

            foreach (Site site in sites) {
                site.Start();
            }

    ---------------------------------
    using System;
    using System.IO;
    using System.DirectoryServices;

    class Class
    {
        static void Main(string[] args)
        {
            DirectoryEntry entry = FindVirtualDirectory("<Server>", "Default Web Site", "<WantedVirtualDir>");
            if (entry != null)
            {
                Console.WriteLine(entry.Properties["AppPoolId"].Value);
            }
        }

        static DirectoryEntry FindVirtualDirectory(string server, string website, string virtualdir)
        {
            DirectoryEntry siteEntry = null;
            DirectoryEntry rootEntry = null;
            try
            {
                siteEntry = FindWebSite(server, website);
                if (siteEntry == null)
                {
                    return null;
                }

                rootEntry = siteEntry.Children.Find("ROOT", "IIsWebVirtualDir");
                if (rootEntry == null)
                {
                    return null;

                }

                return rootEntry.Children.Find(virtualdir, "IIsWebVirtualDir");
            }
            catch (DirectoryNotFoundException ex)
            {
                return null;
            }
            finally
            {
                if (siteEntry != null) siteEntry.Dispose();
                if (rootEntry != null) rootEntry.Dispose();
            }
        }

        static DirectoryEntry FindWebSite(string server, string friendlyName)
        {
            string path = String.Format("IIS://{0}/W3SVC", server);

            using (DirectoryEntry w3svc = new DirectoryEntry(path))
            {
                foreach (DirectoryEntry entry in w3svc.Children)
                {
                    if (entry.SchemaClassName == "IIsWebServer" &&
                        entry.Properties["ServerComment"].Value.Equals(friendlyName))
                    {
                        return entry;
                    }
                }
            }
            return null;
        }
    }

    梦想还是要的,万一实现了呢!
  • 相关阅读:
    js数组与字符串的相互转换
    JS怎么把字符串数组转换成整型数组
    element-UI的操作步骤steps每一项添加事件,比如click,hover
    element-UI ,Table组件实现拖拽效果
    修改本机域名localhost为任意你想要的名称
    el-tree 设置目录树中的某个节点为高亮状态
    Akka-CQRS(2)- 安装部署cassandra cluster,ubuntu-16.04.1-LTS and MacOS mojave
    Akka-CQRS(1)- Write-side, Persisting event sources:CQRS存写端操作方式
    Akka-CQRS(0)- 基于akka-cluster的读写分离框架,构建gRPC移动应用后端架构
    Akka-Cluster(6)- Cluster-Sharding:集群分片,分布式交互程序核心方式
  • 原文地址:https://www.cnblogs.com/cgli/p/2595339.html
Copyright © 2011-2022 走看看