如何使用的Win32类,如Win32

编程入门 行业动态 更新时间:2024-10-21 22:53:15
本文介绍了如何使用的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

本文发布于:2023-11-06 06:47:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562978.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!