static class Program
{
//[STAThread]
static void Main(string[] args)
{
string serverComment = "Default Web Site";
string iisHost = "IIS://LocalHost";
string siteName = GetIISSiteName(iisHost, serverComment);
string virtualDirectory = "BetssonInterface";
Console.WriteLine(siteName);
if (siteName.Equals(String.Empty))
{
Console.WriteLine("Site not found.");
}
else
{
string path = GetVirtualDirPath(iisHost, siteName, virtualDirectory);
if (path.Equals(String.Empty))
Console.WriteLine("VD not found.");
else
Console.WriteLine(path);
}
Console.WriteLine("done");
Console.ReadLine();
}
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);
entry.Invoke("reset", null);
entry.Invoke("AppDelete", null);
entry.Invoke("AppCreate", true);
return entry.Properties["Path"].Value.ToString();
}
catch (Exception ex)
{
// If Virtual Directory is not found,
// it will throw exception.
return "";
}
return "";
}
}
{
//[STAThread]
static void Main(string[] args)
{
string serverComment = "Default Web Site";
string iisHost = "IIS://LocalHost";
string siteName = GetIISSiteName(iisHost, serverComment);
string virtualDirectory = "BetssonInterface";
Console.WriteLine(siteName);
if (siteName.Equals(String.Empty))
{
Console.WriteLine("Site not found.");
}
else
{
string path = GetVirtualDirPath(iisHost, siteName, virtualDirectory);
if (path.Equals(String.Empty))
Console.WriteLine("VD not found.");
else
Console.WriteLine(path);
}
Console.WriteLine("done");
Console.ReadLine();
}
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);
entry.Invoke("reset", null);
entry.Invoke("AppDelete", null);
entry.Invoke("AppCreate", true);
return entry.Properties["Path"].Value.ToString();
}
catch (Exception ex)
{
// If Virtual Directory is not found,
// it will throw exception.
return "";
}
return "";
}
}
Комментарии