/// <summary>
        /// Get the filesystem folder for the given website
        /// </summary>
        /// <param name="serverName">Name of the IIS server e.g. localhost</param>
        /// <param name="websiteName">Name of the website e.g. test</param>
        /// <returns>filesystem folder or empty if not found</returns>
        public string GetWebSiteFileSystemFolder(string serverName, string websiteName)
        {
            if (string.IsNullOrEmpty(serverName)) { throw new Exception("Parameter [serverName] can't be null or empty"); }
            if (string.IsNullOrEmpty(websiteName)) { throw new Exception("Parameter [websiteName] can't be null or empty"); }
            string result = string.Empty;

            DirectoryEntry w3svc = new DirectoryEntry(string.Format("IIS://{0}/w3svc", serverName));

            foreach (DirectoryEntry site in w3svc.Children)
            {
                if (site.Properties["ServerComment"] != null)
                {
                    if (site.Properties["ServerComment"].Value != null)
                    {
                        if (string.Compare(site.Properties["ServerComment"].Value.ToString(), websiteName, false) == 0)
                        {

                            foreach (DirectoryEntry child in site.Children)
                            {
                                if (child.Properties["Path"] != null)
                                {
                                    if (child.Properties["Path"].Value != null)
                                    {
                                        result = child.Properties["Path"].Value.ToString();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return result;
        }
 

Change the Propery “Path” to “ServerBindings” to change serverbindings like “:80:testwebsite”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.