我正在使用WMI Code Creator创建代码以添加网络打印机。
http://img13.imageshack.us/img13/9847/wmicodecreatorwin32prin.png
生成的代码很好用(至少在我的域账户下是这样):using System;
using System.Management;
using System.Windows.Forms;
namespace WMISample
{
public class CallWMIMethod
{
public static void Main()
{
try
{
ManagementClass classInstance =
new ManagementClass("root\\CIMV2",
"Win32_Printer", null);
// Obtain in-parameters for the method
ManagementBaseObject inParams =
classInstance.GetMethodParameters("AddPrinterConnection");
// Add the input parameters.
inParams["Name"] = "\\\\PrintServer\\PrinterName";
// Execute the method and obtain the return values.
ManagementBaseObject outParams =
classInstance.InvokeMethod("AddPrinterConnection", inParams, null);
// List outParams
Console.WriteLine("Out parameters:");
Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
}
}
}
}
然而,我需要将网络打印机添加到本地PC帐户中,即非域帐户,没有访问\PrintServer的权限。
在上述代码中,我应该在哪里输入域用户(服务帐户)的用户名和密码?
我已经谷歌了几个小时,但是我只能找到一个愚蠢的帖子,它说如何在远程计算机上添加打印机,这不是我要做的。
(我需要将远程打印机添加到当前PC,而不是远程PC。)(唯一的限制是登录的用户是本地PC帐户。)
有人知道如何实现这一点吗?