使用ManagementClass将打印机添加到本地计算机 [英] add printer to local computer using ManagementClass

查看:58
本文介绍了使用ManagementClass将打印机添加到本地计算机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一些参考和提示,它们可以通过编程方式使用ManagementClass等将网络打印机添加到本地计算机。但是,我无法找到有关此操作的任何实际教程。

I see references and hints that programmatically one can add a networked printer to a local computer using the ManagementClass and such. However I have not been able to find any actual tutorials on doing just this.

有人真的使用过ManagementClass来做到这一点吗?

has anyone actually used the ManagementClass to do this?

我正在这样做:

var connectionOption = new ConnectionOption();
var mgmScope = new ManagementScope("root\cimv2",connectionOptions);

var printerClass = new ManagementClass(mgmScope, new ManagementPath("Win32_Printer"),null);
var printerObj = printerClass.CreateInstance();

printerObj["DeviceID"] = prnName;     //
printerObj["DriverName"] = drvName;   // full path to driver
printerObj["PortName"] = "myTestPort:";

var options = new PutOptions {Type = PutType.UpdateOrCreate};
printerObj.Put(options);   

这一切都是创建错误 Generic Failure

All this does is create an error "Generic Failure"

我无法弄清我所缺少的.....对此的任何帮助或想法将不胜感激。

I cant figure out what I am missing..... any help or thoughts about this would be appreciated.

我认为我需要更好地解释我要做什么...当所需的打印机未绑定到打印服务器时,我需要:
创建一个tcpip原始端口,
通过tcp / ip连接打印机,
安装驱动程序,
可以选择设置默认值。

I think I need to better explain what I am trying to do... when the printers needed are not tied to a print server, I need to: create a tcpip raw port, connect a printer via tcp/ip, install drivers, optionally set default.

我希望WMI基本上可以解决所有这些问题,但事实并非如此。

I was hoping WMI could basically take care of all of this but it doesnt appear to be the case.

谢谢!

推荐答案

为了做到这一点,我最终不得不做一个2步...

In order to do this I ended up having to do a 2 stepper...

首先建立一个命令行来启动:

first build up a command line to fire off:

rundll32.exe printui.dll,PrintUIEntry /if /b "test" /f x2DSPYP.inf /r 10.5.43.32 /m "845 PS"

然后生成它:

    public static string ShellProcessCommandLine(string cmdLineArgs,string path)
    {
        var sb = new StringBuilder();
        var pSpawn = new Process
        {
            StartInfo =
                {
                    WorkingDirectory = path, 
                    FileName = "cmd.exe", 
                    CreateNoWindow = true,
                    Arguments = cmdLineArgs,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    UseShellExecute = false
                }
        };
        pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
        pSpawn.Start();
        pSpawn.BeginOutputReadLine();
        pSpawn.WaitForExit();

        return sb.ToString();
    }

这似乎行得通……不是理想的方法,但对于那些打印机却不可行在打印服务器上似乎可以完成这项工作。

This seems to work... not the ideal method but for those printers not on a print server it seems to do the job.

这篇关于使用ManagementClass将打印机添加到本地计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆