我必须在C#.net中使用Win32_PrintJob
,Win32_Printer
等Win32类。
我没有在My c#.net应用程序中使用Win32类导入哪个命名空间?
答案 0 :(得分:1)
这些是WMI类,因此您需要使用System.Management
命名空间。
以下是关于打印机的精彩文章:Managing Printers Programatically using C# and WMI
答案 1 :(得分:1)
为了从C#(或.Net)访问WMI,您必须导入System.Management
命名空间。此外,如果您是使用WMI的新手,请尝试WMI Delphi Code Creator
之类的工具,它可以帮助您创建一个C#代码段来访问WMI。
尝试使用该工具创建的示例代码。
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();
}
}
}
答案 2 :(得分:0)
确实在.NET中实现了这样的对象,因此您不需要使用这些对象 如果你必须直接使用winapi,你可以创建一个结构,其中包含winapi结构中包含的所有内容(它们是结构,而不是类)。
一些注意事项:
保持所有成员的确切顺序和类型
用System.IntPtr
表示句柄(例如HWND等)
如果您需要传递结构的大小,请使用Marshal.SizeOf()
您还可以添加[StructLayout]
属性,您可以在其中指定字符集和其他重要属性
命名空间System.Runtime.InteropServices
有一些常见的winapi结构。
答案 3 :(得分:0)
虽然您尚未标记答案,但可能您正在寻找能够为您提供.NET框架命名空间System.Management
的其他答案。
但是, 可能是您一直在阅读WMI文档,而您是从这个角度出发的。其中一个基本概念是 WMI命名空间,它当然与.NET命名空间完全不同。所以这是一个完全不同的答案,以防万一...
您可能需要的#1 WMI名称空间为root\cimv2
。只有当你深入挖掘时,你才可以使用root\subscription
或某个公司选择的命名空间,这些公司决定将他们自己的WMI提供者置于“特殊”的位置。
通常,当您想要连接到WMI名称空间时,它位于计算机名称之后,例如: \\<computer>\root\cimv2
。在本地连接时,可以省略计算机,因此您只需放置命名空间路径,例如root\cimv2
在.NET中,您可以通过在字符串中使用它来构建ManagementPath
来指定WMI命名空间,或者在传递给ManagementScope
构造函数的字符串中隐式指定WMI命名空间。