C#/.NET : How to add networked printer to a local PC account?

标签 c# .net windows printers

我正在使用 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 帐户。)

有人知道如何实现吗?

最佳答案

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

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

在 pc1 上以 bob1 运行你的 map 程序,你应该能够访问打印机。

这有帮助吗?

否则,网络打印机是针对每个用户的...以具有访问权限(即 runas)的域用户身份运行您的程序是行不通的,因为它只会将打印机映射到用户 session ,而不是您真正想要的 session 。

...这个呢? 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 的东西)

关于C#/.NET : How to add networked printer to a local PC account?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6963152/

相关文章:

c# - 为什么TimeSpan和Guid Structs可以比作null?

.net - HTMLAgilityPack .load 连接在某些站点上关闭

c# - 如果文件已损坏,请忽略该文件

c# - 启动外部应用程序时创建等待光标

.net - 线程vs线程池-.Net 2.0

.net - Apple 会将 Mono Touch 运行时与每部 iPhone 捆绑在一起吗?

c++ - 如何缩短命令行命令?

windows - Postgresql:无法建立连接,因为目标机器主动拒绝它

c++ - 在 MySql 中使用字符串操作函数会出现什么问题?

c# - NuGet 包将非托管 dll 复制到根目录而不是 bin