其它的属性大多都可以显示,跟电脑系统应该没有关系的,我在家用win7 也试过的,我现在的是server2003
ManagementObjectCollection printers = new ManagementClass("Win32_Printer").GetInstances(); foreach (ManagementObject printer in printers) { StringBuilder l_strBuild = new StringBuilder(); foreach(System.Management.PropertyData property in printer.Properties) { l_strBuild.Append(string.Format("\t{0}: {1}", property.Name, property.Value)); } }
或者
string query = string.Format("SELECT * from Win32_Printer "); var searcher = new ManagementObjectSearcher(query); var printers = searcher.Get(); foreach (var printer in printers) { if (printer.Properties["Name"].Value.ToString() != "BTP-M280(P)") { StringBuilder l_strBuilder = new StringBuilder(); foreach (var property in printer.Properties) { l_strBuilder.Append(string.Format("\r\t{0}: {1}", property.Name, property.Value)); } } }
Availability 为 uint16 类型,是如何出现 null 值的?
取出来就是这个样子的值?
Attributes: 588
Availability:
AvailableJobSheets:
AveragePagesPerMinute: 0
Capabilities: System.UInt16[]
CapabilityDescriptions: System.String[]
Caption: BTP-M280(P)
CharSetsSupported:
Comment:
ConfigManagerErrorCode:
ConfigManagerUserConfig:
CreationClassName: Win32_Printer
CurrentCapabilities:
CurrentCharSet:
CurrentLanguage:
CurrentMimeType:
CurrentNaturalLanguage:
CurrentPaperType:
Default: True
DefaultCapabilities:
DefaultCopies:
DefaultLanguage:
@maanshancss: 看一下property.Name 为 Availability 时, property.Value 的类型。
@Launcher: 就是null 不能转换为string 类型,如果转就报错,未将对象引用到对象实例
@Launcher:
@maanshancss: 测试了下,的确是这样。你可以按照值为 1 来处理。
@Launcher: 那样不合我的要求了,算了把分给你吧!
我用命令判断的!