我正在使用 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 帐户。)
有谁知道如何做到这一点?