C# 使用ManagementClass添加ScriptMap对象
我已经问了一个相关的问题,但遗憾的是,答案虽然正确,但实际上并没有解决我的问题 我使用的是ManagementClass/ManagementObject WMI API(因为它在处理远程管理方面比DirectoryEntry API更好)。我想完全删除现有的脚本映射 使用通用字符串格式解决方案似乎适用于VBS,但不适用于ManagementClass API。所以,我一直在尝试写一些东西来创建正确的脚本映射对象数组,例如C# 使用ManagementClass添加ScriptMap对象,c#,wmi,C#,Wmi,我已经问了一个相关的问题,但遗憾的是,答案虽然正确,但实际上并没有解决我的问题 我使用的是ManagementClass/ManagementObject WMI API(因为它在处理远程管理方面比DirectoryEntry API更好)。我想完全删除现有的脚本映射 使用通用字符串格式解决方案似乎适用于VBS,但不适用于ManagementClass API。所以,我一直在尝试写一些东西来创建正确的脚本映射对象数组,例如 foreach (var extension in extens
foreach (var extension in extensions) {
var scriptMap = scriptMapClass.CreateInstance();
SetWmiProperty(scriptMap, "ScriptMap.Extensions", "." + extension);
不幸的是,似乎无法实现函数SetWmiProperty。如果我尝试以下操作
wmiObject.Properties.Add(propertyName, CimType.SInt32);
我得到“由于对象的当前状态,操作无效。”。另一方面,如果我只是尝试设置属性,我会被告知该属性不存在。scriptMap类具有路径“scriptMap”,这是现有对象显示的路径
是否有人拥有使用ManagementClass API操纵脚本映射的工作代码?我发现从头开始创建WMI对象非常困难。更容易克隆()从系统查询的现有对象,然后对其进行修改。这是我最近编写的一个函数,用于处理脚本映射。它在Powershell中,不是在C#中,但想法是一样的:
function Add-AspNetExtension
{
[CmdletBinding()]
param (
[Parameter(Position=0, Mandatory=$true)]
[psobject] $site # IIsWebServer custom object created with Get-IIsWeb
[Parameter(ValueFromPipeline=$true, Mandatory=$true)]
[string] $extension
)
begin
{
# fetch current mappings
# without the explicit type, PS will convert it to an Object[] when you use the += operator
[system.management.managementbaseobject[]] $maps = $site.Settings.ScriptMaps
# whatever the mapping is for .aspx will be our template for mapping other things to ASP.NET
$template = $maps | ? { $_.Extensions -eq ".aspx" }
}
process
{
$newMapping = $template.Clone()
$newMapping.Extensions = $extension
$maps += newMapping
}
end
{
$site.Settings.ScriptMaps = $maps
}
}
理查德·伯格(Richard Berg)概述的这项技术的一个C#示例
static void ConfigureAspNet(ManagementObject virtualDirectory, string version, string windowsLocation, IEnumerable<string> extensions)
{
var scriptMaps = virtualDirectory.GetPropertyValue("ScriptMaps");
var templateObject = ((ManagementBaseObject[])scriptMaps)[0];
List<ManagementBaseObject> result = new List<ManagementBaseObject>();
foreach (var extension in extensions) {
var scriptMap = (ManagementBaseObject) templateObject.Clone();
result.Add(scriptMap);
if (extension == "*")
{
scriptMap.SetPropertyValue("Flags", 0);
scriptMap.SetPropertyValue("Extensions", "*");
} else
{
scriptMap.SetPropertyValue("Flags", 5);
scriptMap.SetPropertyValue("Extensions", "." + extension);
}
scriptMap.SetPropertyValue("IncludedVerbs", "GET,HEAD,POST,DEBUG");
scriptMap.SetPropertyValue("ScriptProcessor",
string.Format(@"{0}\microsoft.net\framework\{1}\aspnet_isapi.dll", windowsLocation, version));
}
virtualDirectory.SetPropertyValue("ScriptMaps", result.ToArray());
virtualDirectory.Put();
}
静态void配置Aspnet(ManagementObject虚拟目录、字符串版本、字符串窗口位置、IEnumerable扩展名)
{
var scriptMaps=virtualDirectory.GetPropertyValue(“scriptMaps”);
var templateObject=((ManagementBaseObject[])脚本映射)[0];
列表结果=新列表();
foreach(扩展中的var扩展){
var scriptMap=(ManagementBaseObject)templateObject.Clone();
结果.添加(脚本映射);
如果(扩展名=“*”)
{
scriptMap.SetPropertyValue(“标志”,0);
scriptMap.SetPropertyValue(“扩展名”,“*”);
}否则
{
scriptMap.SetPropertyValue(“标志”,5);
scriptMap.SetPropertyValue(“扩展名”,“扩展名+扩展名”);
}
SetPropertyValue(“IncludedVerbs”、“GET、HEAD、POST、DEBUG”);
scriptMap.SetPropertyValue(“ScriptProcessor”,
string.Format(@“{0}\microsoft.net\framework\{1}\aspnet_isapi.dll”,windowsLocation,version));
}
SetPropertyValue(“ScriptMaps”,result.ToArray());
virtualDirectory.Put();
}
正是我所需要的。非常感谢:这解决了我两年来一直很烦恼的问题。