我使用WMICodeCreator在visual studio中创建了这个小小的C#win32控制台应用程序:
using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class MyWMIQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_ServerSession");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_ServerSession instance");
Console.WriteLine("-----------------------------------");
Console.WriteLine("ActiveTime: {0}", queryObj["ActiveTime"]);
Console.WriteLine("Caption: {0}", queryObj["Caption"]);
Console.WriteLine("ClientType: {0}", queryObj["ClientType"]);
Console.WriteLine("ComputerName: {0}", queryObj["ComputerName"]);
Console.WriteLine("Description: {0}", queryObj["Description"]);
Console.WriteLine("IdleTime: {0}", queryObj["IdleTime"]);
Console.WriteLine("InstallDate: {0}", queryObj["InstallDate"]);
Console.WriteLine("Name: {0}", queryObj["Name"]);
Console.WriteLine("ResourcesOpened: {0}", queryObj["ResourcesOpened"]);
Console.WriteLine("SessionType: {0}", queryObj["SessionType"]);
Console.WriteLine("Status: {0}", queryObj["Status"]);
Console.WriteLine("TransportName: {0}", queryObj["TransportName"]);
Console.WriteLine("UserName: {0}", queryObj["UserName"]);
}
}
catch (ManagementException e)
{
MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
}
}
}
}
这就是我得到的:拒绝访问。即使我以管理员身份运行它。
为什么我会“拒绝访问”?