1

I want to be able to check if an application exists within an existing website but I've not found anything. At the moment I have an installer project that gets the input from the user for the name of an existing website. I use this to check the site currently exists in IIS, and if so I then want to search the existing applications to see if a particular 1 exists. Here's what I have:

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.");
    }
}

Obviously app.Name doesn't exist, so what can I do to get this?

sr28
  • 4,728
  • 5
  • 36
  • 67

1 Answers1

2

You can use the Microsoft.Web.Administration and Microsoft.Web.Management API for IIS to do this. They are not in the GAC though, you have to reference them from the inetsrv folder. On my machine they are located at...

  1. C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll
  2. C:\Windows\System32\inetsrv\Microsoft.Web.Management.dll

Here is some example code of enumerating them,

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);
    }
}

Follow Up:

Applications do not have names, only the root Site has a name in IIS. Applications only have a path (as they are children of sites). If the Path is "/" then the Application is the root application for the site. If the path is anything other than / then it is a 2nd+ child application of the site. So you would need to use Application.Path to do what you want.

Ryan Mann
  • 5,178
  • 32
  • 42
  • Thanks for the answer. I know you need those references (hence why my code has Microsoft.Web.Administration.Application app in apps). Basically, are you saying the name of the Application is displayed by the property 'Path'? – sr28 Jan 12 '15 at 16:12
  • I updated my answer when I saw that, but yes. Applications do not have names, they have paths. – Ryan Mann Jan 12 '15 at 16:13
  • Right ok. Thanks for the follow up bit. So for my purposes if I have an application within a site called 'MyApp' then I could just check against the application.path == "MyApp"? – sr28 Jan 12 '15 at 16:15
  • IIS is confusing because in IIS it is called an Alias, but in the API it is "Path". So yes, you should check where path == "MyApp". – Ryan Mann Jan 12 '15 at 16:17
  • Also, just mentioning this in case you run into it. Depending on what you do with this API, the user executing the code in some scenarios must be an administrator. So if you get this code running in an Web Site (w3wp.exe) process and you start getting permission issues, consider impersonating an admin on the code, or switching the application pool identity to an IIS Admin. – Ryan Mann Jan 12 '15 at 16:20
  • Ran into that before, but shouldn't be a problem. It's basically part of an installer project to be run by admins and make their lives easier. – sr28 Jan 12 '15 at 16:22