Search code examples
c#.netweb-servicesregistry

How to find the location of a web application root directory from within my application?


List of installed applications from Registry, are often, retrieved from here

Software\Microsoft\Windows\CurrentVersion\Uninstall

The location of the programs can be retrieved from the relavent subkey "InstallLocation". However, this seems to not apply to windows and web services.

Windows Services location can be retrieved from

SYSTEM\CurrentControlSet\Services

How can I find information about Web Services and their location from registry?


Solution

  • The Microsoft.Web.Administration.dll can be used to query IIS, you can do things like,

    1. Get List of all websites
    2. Get info about a specific Website
    3. Get info about web applications in a website
    4. Get info about the binding in an application
    5. Create/Modify, etc a website ....

    A simple usage example would be

    private ServerManager serverManager
            {
                get
                {
                    if (_serverManager == null)
                            _serverManager = new ServerManager();
    
    
                    return _serverManager;
                }
                set { _serverManager = value; }
            }
    

    and you can use the above variable to get a specific site name

    var site = serverManager.Sites.First(x => x.Name.ToLower() == siteName.ToLower());
    

    where you can get the site names from serverManager.Sites

    You can also open application webconfigs and modify,query,etc

    public static System.Configuration.Configuration GetConfigurationFrom(string appName, string hostName, string site)
            {
                return System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(@"/" + appName, site, null, hostName);
            }