C#/ .NET:如何将联网打印机添加到本地PC帐户?

Joh*_*ets 8 .net c# windows printers

我正在使用WMI代码创建器来创建添加联网打印机的代码.

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我需要将网络打印机添加到本地PC帐户,即无法访问\ PrintServer的非域帐户.

我在哪里可以将域用户(服务帐户)用户名和密码放入上面的代码中?

我一直在谷歌搜索几个小时,但我能找到的是一个愚蠢的帖子,说明如何在远程机器上添加打印机,这不是我想要做的.

(我需要将远程打印机添加到当前 PC,而不是远程PC.)(需要注意的是登录用户是本地PC帐户.)

有谁知道如何实现这一目标?

小智 1

您可以在打印服务器上创建相同的本地帐户,从而启用对等身份验证...

即 pc1 在本地有用户 bob1。使 bob1 成为打印服务器上的用户。

在 pc1 上以 bob1 身份运行地图程序,您应该能够访问打印机。

有帮助吗?

否则,网络打印机是每个用户的...作为具有访问权限的域用户(即 runas)运行您的程序将无法工作,因为它只是将打印机映射到用户会话,而不是您真正想要的会话。

...那这个呢? http://www.codescript.co.uk/wmi_connect_as_another_user.htm

...还是脚本式的? http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=12028 (即使它不适用于 c#,它仍然可以提供 wmi synatx 的东西)