ManagementObjectSearcher Get()方法:“内存不足,无法继续执行程序。”

时间:2013-07-26 11:34:18

标签: .net c#-4.0 wmi

  • 我在这里阅读所有相关答案都没有帮助。
  • 另请注意:如果我在VS 2012调试器中运行该程序,它就像一个魅力。 但是,如果我在没有调试(ctrl F5)的情况下运行或在VS 2012 ID之外运行.exe,则会抛出异常。
  • 我关闭了防火墙
  • 我以管理员身份运行exe
  • 我重新启动了WMI服务。

相同..

这是代码。 .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);
            }
        }
    }
}

2 个答案:

答案 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);
    }
}
相关问题