I've already asked a related question, but sadly the answers, whilst correct, didn't actually solve my problem.
I'm using the ManagementClass/ManagementObject WMI API (because it's better at handling remote administration than the DirectoryEntry API). I'd like to completely remove the existing script maps from a
Using the common string format solution seems to work for VBS, but not for the ManagementClass API. So, I've been trying to write something that would create the correct array of script map objects e.g.
foreach (var extension in extensions) {
var scriptMap = scriptMapClass.CreateInstance();
SetWmiProperty(scriptMap, "ScriptMap.Extensions", "." + extension);
Unfortunately, it doesn't seem possible to implement the function SetWmiProperty. If I attempt the following
wmiObject.Properties.Add(propertyName, CimType.SInt32);
I get "Operation is not valid due to the current state of the object.". On the other hand, if I just try to set the property, I get told that the property doesn't exist. The scriptMap class has the path "ScriptMap", which is what the existing objects display.
Does anyone have any working code that manipulates ScriptMaps using the ManagementClass API?
A C# example of the technique outlined by Richard Berg.
I find it's very difficult to create WMI objects from scratch. Easier to Clone() an existing object you've queried from the system and then modify it. Here's a function I wrote recently to deal with ScriptMaps. It's in Powershell, not C#, but the idea is the same: