在ManagementObject中查找可用数据

时间:2013-12-14 13:36:00

标签: c# .net printing

我正在使用ManagementObjectSearcher加载网络中可用的所有打印机。所有打印机都在ManagementObjectCollection中返回。反正有没有找到返回的所有细节?

我使用c#的调试来预览对象,但它没有显示那里的所有数据。我想知道打印机[名称],打印机[本地],打印机[网络]以外的可用内容。有可能这样做吗?

代码

System.Management.ManagementScope objMS =
            new System.Management.ManagementScope(ManagementPath.DefaultPath);
        objMS.Connect();

        SelectQuery objQuery = new SelectQuery("SELECT * FROM Win32_Printer");
        ManagementObjectSearcher objMOS = new ManagementObjectSearcher(objMS, objQuery);
        System.Management.ManagementObjectCollection objMOC = objMOS.Get();
        foreach (ManagementObject Printers in objMOC)
        {
            System.Management.PropertyDataCollection pdc = Printers.Properties;
            if (Convert.ToBoolean(Printers["Local"]))       // LOCAL PRINTERS.
            {
                comboBox8.Items.Add(Printers["Name"]);
            }
            if (Convert.ToBoolean(Printers["Network"]))     // ALL NETWORK PRINTERS.
            {
                comboBox9.Items.Add(Printers["Name"]);
            }
        }

2 个答案:

答案 0 :(得分:1)

为Win32_Printer生成强类型类,它将显示您需要的所有内容。

http://msdn.microsoft.com/en-us/library/2wkebaxa(v=vs.110).aspx - Mgmtclassgen.exe(管理强类型类生成器)

答案 1 :(得分:1)

您可以枚举ManagementBaseObject.Properties属性和PropertyData.Qualifiers

    foreach (PropertyData property in properties)
    {
        Console.WriteLine(property.Name);

        foreach (QualifierData q in property.Qualifiers)
        {
            if(q.Name.Equals("Description"))
            {
                Console.WriteLine(
                    processClass.GetPropertyQualifierValue(
                    property.Name, q.Name));
            }

        }
        Console.WriteLine();

    }

来自MSDN