如何使用的Win32类,如Win32_Printer? [英] How to use Win32 classes like Win32_Printer?
本文介绍了如何使用的Win32类,如Win32_Printer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要使用的Win32类,如 Win32_PrintJob
, Win32_Printer
等在C#.NET。
I have to use Win32 classes like Win32_PrintJob
, Win32_Printer
etc in C#.net.
我没有得到我应该导入我的C#.NET应用程序中使用的Win32类哪个命名空间?
I am not getting which namespace I should import to use Win32 classes in My c#.net 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_Printer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文