使用 ManagementClass 将打印机添加到本地计算机 [英] add printer to local computer using 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("rootcimv2",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);
所有这些都会产生一个错误通用故障"
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 stepper...
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屋!