本文介绍了如何使用的Win32类,如Win32_Printer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要使用的Win32类,如 Win32_PrintJob , Win32_Printer 等在C#.NET。
I have to use Win32 classes like Win32_PrintJob, Win32_Printer etc in C#.
我没有得到我应该导入我的C#.NET应用程序中使用的Win32类哪个命名空间?
I am not getting which namespace I should import to use Win32 classes in My c# application?
推荐答案为了获得从C#(或.NET)访问WMI必须导入的 System.Management 命名空间。此外,如果您是初次使用WMI尝试像 WMI Delphi代码造物主 它可以帮助你创建一个C#代码段访问WMI。
In order to get access to the WMI from C# (or .Net) you must import the System.Management namespace. Also if you are new using the WMI try a tool like the WMI Delphi Code Creator which can help you to create a C# snippet to access the WMI.
试试这个示例代码,通过该工具创建
Try this sample code, created by the tool.
using System; using System.Collections.Generic; using System.Management; using System.Text; namespace GetWMI_Info { class Program { static void Main(string[] args) { try { string ComputerName = "localhost"; ManagementScope Scope; if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) { ConnectionOptions Conn = new ConnectionOptions(); Conn.Username = ""; Conn.Password = ""; Conn.Authority = "ntlmdomain:DOMAIN"; Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn); } else Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null); Scope.Connect(); ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_PrintJob"); ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query); foreach (ManagementObject WmiObject in Searcher.Get()) { Console.WriteLine("{0,-35} {1,-40}","Document",WmiObject["Document"]);// String Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);// String } } catch (Exception e) { Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace)); } Console.WriteLine("Press Enter to exit"); Console.Read(); } } }更多推荐
如何使用的Win32类,如Win32
发布评论