如何在网站中获取网站应用程序名称?

时间:2015-01-12 16:01:06

标签: c# iis-7 sitecollection

我希望能够检查现有网站中是否存在某个应用程序,但我找不到任何内容。目前我有一个安装程序项目,它从用户那里获取现有网站名称的输入。我使用它来检查IIS中当前存在的站点,如果是,那么我想搜索现有的应用程序以查看是否存在特定的1。这就是我所拥有的:

private void CheckWebsiteApps()
{
    SiteCollection sites = null;

    try
    {
        //Check website exists
        sites = mgr.Sites;
        foreach (Site s in sites)
        {
            if (!string.IsNullOrEmpty(s.Name))
            {
                if (string.Compare(s.Name, "mysite", true) == 0)
                {
                    //Check if my app exists in the website
                    ApplicationCollection apps = s.Applications;
                    foreach (Microsoft.Web.Administration.Application app in apps)
                    {
                        //Want to do this
                        //if (app.Name == "MyApp")
                        //{ 
                        //    Do Something
                        //}
                    }
                }
            }
        }
    }
    catch
    {
        throw new InstallException("Can't determine if site already exists.");
    }
}

显然app.Name不存在,那么我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

您可以使用Microsoft的Microsoft.Web.Administration和Microsoft.Web.Management API来执行此操作。但它们不在GAC中,您必须从inetsrv文件夹中引用它们。在我的机器上,它们位于......

  1. C:\ Windows \ System32下\ INETSRV \ Microsoft.Web.Administration.dll
  2. C:\ Windows \ System32下\ INETSRV \ Microsoft.Web.Management.dll
  3. 以下是枚举它们的一些示例代码,

    class Program
    {
        static void Main(string[] args)
        {
            ServerManager serverManager = new ServerManager();
            foreach (var site in serverManager.Sites)
            {
                Console.WriteLine("Site -> " + site.Name);
                foreach (var application in site.Applications)
                {
                    Console.WriteLine("  Application-> " + application.Path);
                }
            }
    
    
            Console.WriteLine("Press any key...");
            Console.ReadKey(true);
        }
    }
    

    跟进:

    应用程序没有名称,只有根站点在IIS中有名称。应用程序只有一个路径(因为它们是站点的子项)。如果Path是“/”,那么Application是站点的根应用程序。如果路径不是/然后它是该站点的第二个+子应用程序。因此,您需要使用Application.Path来执行您想要的操作。