简体   繁体   English

简单的C#WMI获取和放置

[英]Simple C# WMI Get & Put

I am trying to Read & Put values from and to WMI using C#. 我正在尝试使用C#从WMI读取和放入值。

The current example uses ccm namespace, for configmgr client. 当前示例对configmgr客户端使用ccm名称空间。

The read functions works correctly, able to read ADV_RepeatRunBehavior value. 读取功能正常工作,能够读取ADV_RepeatRunBehavior值。 Though the Put(); 尽管Put(); doesn't work as expected, the values are not stored back and Invalid Class exception is thrown. 不能按预期方式工作,不会将值存储回去,并且会抛出Invalid Class异常。

Some advice would be nice as I am new to this, many thanks. 由于我是新来的,所以一些建议会很好,非常感谢。

static void Main(string[] args)
{
    try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            "root\\ccm\\Policy\\Machine", 
            "SELECT * FROM CCM_SoftwareDistribution WHERE PKG_PackageID='XXXXXXXX'");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            //Read works
            //Console.WriteLine(queryObj["ADV_RepeatRunBehavior"].ToString());
            //Console.ReadLine();

            //Put doesn't
            queryObj["ADV_RepeatRunBehavior"] = "RerunNever";
            queryObj.Put();
        }
    }
    catch (ManagementException z)
    {
        Console.WriteLine("An error occurred: " + z.Message);
        Console.ReadLine();
    }
}

Found the resolution for this. 找到了解决方法。

  1. You have to run Visual Studio as Administrator if testing on localhost 如果在本地主机上进行测试,则必须以管理员身份运行Visual Studio
  2. The connection to WMI has to be \\\\root\\\\ccm\\\\Policy\\\\Machine\\\\ActualConfig then its possible to Put() values. 与WMI的连接必须为\\\\root\\\\ccm\\\\Policy\\\\Machine\\\\ActualConfig然后才能设置为Put()值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM