相同..
这是代码。 .Get()方法调用
抛出异常注意:调试屏幕不运行调试模式,它来自运行可执行文件,并在连接调试器后崩溃。
http://screencast.com/t/nfvrfz2Hq6Q
同样,如果我在调试中运行该程序,它会像魅力一样运行。
internal static class Program
{
private static void Main(string[] args)
{
var objSearcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_SoundDevice");
var objCollection = objSearcher.Get();
foreach (var obj in objCollection)
{
foreach (var property in obj.Properties)
{
Debug.WriteLine("{0}:{1}", property.Name, property.Value);
}
}
}
}
答案 0 :(得分:0)
是的我可以重现它,你的问题应该是权限,以管理员身份运行程序,在我的情况下它可以工作。
答案 1 :(得分:0)
这是因为"选项"设置为默认模拟。
请尝试以下
public String operater(int arg1, int arg2) throws IllegalArgumentException
{
int quotient;
int remainder;
String resString;
// Check for Divide by 0 Error.
if(arg2 == 0)
{
throw new IllegalArgumentException("Illegal Argument!");
}
else
{
quotient = arg1 / arg2;
remainder = arg1 % arg2;
resString = "Quotient: " + Integer.toString(quotient) +
Remainder: " + Integer.toString(remainder);
}
return resString;
}
现在只需致电:
internal static class WmiQuery
{
public static ManagementScope getWmiConnection(string ipAddress, string username = "", string password = "", string domain = "")
{
//New ConnectionOptions.
ConnectionOptions options = new ConnectionOptions();
bool isLocalhost = (ipAddress.ToLower() == "localhost" || ipAddress == "127.0.0.1");
//ConnectionOptions'Properties invullen.
options.Impersonation = isLocalhost ? ImpersonationLevel.Default : ImpersonationLevel.Impersonate;
options.Username = string.Format(@"{0}\{1}", domain, username);
options.Password = password;
ManagementScope scope = isLocalhost
? new ManagementScope("\\\\" + ipAddress + "\\root\\cimv2")
: new ManagementScope("\\\\" + ipAddress + "\\root\\cimv2", options);
return scope;
}
public static ManagementObjectCollection SearcherGet(this ManagementScope scope, string query)
{
//Query system for Operating System information
if (!scope.IsConnected) scope.Connect();
var queryResult = new ManagementObjectSearcher(scope, new ObjectQuery(query)).Get();
return queryResult;
}
public static void DoWork()
{
var wmiConn = WmiQuery.getWmiConnection("localhost");
ManagementObjectCollection results = wmiConn.SearcherGet("SELECT * FROM Win32_SoundDevice");
foreach (var soundDevice in results)
foreach (var sdProperty in soundDevice.Properties)
Console.WriteLine("{0}:{1}", sdProperty.Name, sdProperty.Value);
}
}