Ermittelt die Id über den Namen der WebSite.
Diese Id findet sich z.B. wieder im Verzeichnisnamen für die IIS LogFiles. Außerdem wird sie benötigt, um manche Einstellungen programmatisch zu ändern.
/// <summary>
/// Get website id on <paramref name="websiteName"/>
/// </summary>
/// <param name = "websiteName">Name of the website e.g. test</param>
/// <returns>
/// Less the 0, site does not exist
/// Id of the existing site
/// </returns>
public long GetWebSiteId(string websiteName)
{
long result = -1;
using (var w3svc = new DirectoryEntry("IIS://localhost/w3svc"))
{
w3svc.RefreshCache();
foreach (DirectoryEntry site in w3svc.Children)
{
using (site)
{
site.RefreshCache();
if (site.Properties["ServerComment"] != null && site.Properties["ServerComment"].Value != null)
{
if (site.Properties["ServerComment"].Value.ToString().Equals(websiteName, StringComparison.OrdinalIgnoreCase))
long.TryParse(site.Name, out result);
}
}
}
}
return result;
}
Kommentare zum Snippet