Monday, July 13, 2009

Get Virtual Dir Path of WebApplication

static string GetIISSiteName(string iisHost, string serverComment) { string adsiPath = iisHost + "/W3SVC"; DirectoryEntry entry = new DirectoryEntry(adsiPath); foreach (DirectoryEntry site in entry.Children) { if (site.SchemaClassName == "IIsWebServer" && site.Properties["ServerComment"].Value.ToString().Equals(serverComment)) { return site.Name; } } return ""; } static string GetVirtualDirPath(string iisHost,string siteName, string vdName) { string adsiPath = iisHost + "/W3SVC/" + siteName + "/Root/" + vdName; try { DirectoryEntry entry = new DirectoryEntry(adsiPath); return entry.Properties["Path"].Value.ToString(); } catch(Exception ex) { // If Virtual Directory is not found, // it will throw exception. return ""; } return ""; }private void Form1_Load(object sender, System.EventArgs e){ string serverComment = "Default Web Site"; string iisHost = "IIS://LocalHost"; string siteName = GetIISSiteName(iisHost, serverComment); MessageBox.Show(siteName); if(siteName.Equals(String.Empty)) { MessageBox.Show("Site not found."); } else { // Get the physical path for http://localhost/testemail string path = GetVirtualDirPath(iisHost, siteName, @"TestVDPath"); if(path.Equals(String.Empty)) MessageBox.Show("VD not found."); else MessageBox.Show(path); } }

Hope this helps

No comments:

Post a Comment