在c#

时间:2017-08-23 13:22:00

标签: c# wmi windows-10-iot-core

我试图以Windows 10 IoT作为用户获取UWF Wilter的状态。我使用以下代码:

private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            ConnectionOptions Options = PrepareOptions();

            ManagementScope scope = new ManagementScope();
            scope.Path = new ManagementPath(@"\\localhost\root\StandardCimv2\embedded");
            scope.Options = Options;
            scope.Connect();

            using (ManagementClass mc = new ManagementClass(scope.Path.Path, "UWF_Filter", null))
            {
                //next line failes with Access Denied under normal user account
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    string UWFstate = (bool)mo.GetPropertyValue("CurrentEnabled");
                    if ( UWFstate )
                    {
                        // Do Something When Enabled
                    }
                    else
                    {
                        // Do Something Else When Disabled
                    }
                }
            }
        }
        catch (Exception ex)
        {
            trow;
        }
    }       


public static ConnectionOptions PrepareOptions()
    {
        ConnectionOptions options = new ConnectionOptions();
        options.Impersonation = ImpersonationLevel.Impersonate;
        options.Authentication = AuthenticationLevel.Default;
        options.EnablePrivileges = true;
        return options;
    }

当我尝试在普通用户帐户下运行此代码并启用了写过滤器时,getinstances()方法返回Access Denied。使用写过滤器禁用代码运行正常。此外,当在管理用户下运行时,它可以完美运行。

当我使用PowerShell尝试此操作时,我还会收到“拒绝访问”消息。

UWF_Filter类的MSDN文档声明:

  

您必须使用管理员帐户对UWF的配置设置进行更改。拥有任何类型帐户的用户都可以读取当前配置设置。

所以我认为普通用户必须能够读取UWF配置。或者我在某个地方犯了错误?

有什么想法吗?

谢谢米歇尔。

1 个答案:

答案 0 :(得分:1)

我有同样的问题。经过大量研究后,最终发现WMI甚至不为管理员提供访问权限,即使管理员在UWF_Volume中写入排除项(通过PowerShell,C#,C ++从管理员帐户尝试),而UWF_Filter.Enable(),UWF_Filter.Disable()和其他一些方法也无法工作正确地。 因此,因此要添加排除,我在程序中使用了命令工具。