Grz*_*nio 10 .net c# iis installer wmi
我正在为我的Web服务编写安装程序类.在许多情况下,当我使用WMI时(例如,在创建虚拟目录时),我必须知道siteId为站点提供正确的metabasePath,例如:
metabasePath is of the form "IIS://<servername>/<service>/<siteID>/Root[/<vdir>]"
for example "IIS://localhost/W3SVC/1/Root"
Run Code Online (Sandbox Code Playgroud)
如何根据站点名称(例如"默认网站")以编程方式在C#中查找?
Gle*_*lar 12
以下是如何通过名称获取它.您可以根据需要进行修改.
public int GetWebSiteId(string serverName, string websiteName)
{
int result = -1;
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)
{
result = int.Parse(site.Name);
break;
}
}
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
您可以通过检查搜索的网站ServerComment
属于数据库路径的子女财产IIS://Localhost/W3SVC
有一个SchemaClassName
的IIsWebServer
.
以下代码演示了两种方法:
string siteToFind = "Default Web Site";
// The Linq way
using (DirectoryEntry w3svc1 = new DirectoryEntry("IIS://Localhost/W3SVC"))
{
IEnumerable<DirectoryEntry> children =
w3svc1.Children.Cast<DirectoryEntry>();
var sites =
(from de in children
where
de.SchemaClassName == "IIsWebServer" &&
de.Properties["ServerComment"].Value.ToString() == siteToFind
select de).ToList();
if(sites.Count() > 0)
{
// Found matches...assuming ServerComment is unique:
Console.WriteLine(sites[0].Name);
}
}
// The old way
using (DirectoryEntry w3svc2 = new DirectoryEntry("IIS://Localhost/W3SVC"))
{
foreach (DirectoryEntry de in w3svc2.Children)
{
if (de.SchemaClassName == "IIsWebServer" &&
de.Properties["ServerComment"].Value.ToString() == siteToFind)
{
// Found match
Console.WriteLine(de.Name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这假定ServerComment
已使用该属性(IIS MMC强制使用它)并且是唯一的.
归档时间: |
|
查看次数: |
12919 次 |
最近记录: |