【问题标题】:How can I install/add a virtual printer using c# commands?如何使用 c# 命令安装/添加虚拟打印机?
【发布时间】:2019-04-02 10:56:56
【问题描述】:

我正在尝试使用 Windows 桌面应用程序安装虚拟打印机(适用于所有版本的 Windows)。我希望这台打印机在打印对话框的下拉列表中可用(尤其是从 Office)。

我正在尝试使用此代码:

public static void installPrinter(string printerName)
{
    string arg;

    arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + @" /f C:\Windows\inf\ntprint.inf /r " + "\"" + "lpt1:" + "\"" + " /m " + "\"" + "Brother DCP-116C" + "\""; //initial args
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = "rundll32.exe";
    p.Arguments = arg;
    p.WindowStyle = ProcessWindowStyle.Hidden;

    try
    {
        Process.Start(p);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.InnerException.ToString());
    }
}

但它不起作用。有什么建议吗?

【问题讨论】:

  • 异常信息是什么?你确定这将/应该在每个 Windows 版本上运行。从 Windows 3.11 到具有所有不同 API/框架的 Windows 10?
  • “它不起作用”不是我们可以为您提供帮助的问题陈述。
  • @Patric 没有异常消息,只有经典的 windows 声音(弹出以管理员身份运行时发出的声音)。我不确定这一点,但我会很高兴至少让它在一个版本上工作。这是我第一次尝试做这样的事情,我不确定它是如何工作的。可能我必须将驱动程序嵌入到我的项目中?很抱歉描述不好,但我一直在想它,而且我的英语不是最好的。提前谢谢
  • @IanKemp 抱歉,我的问题可能不仅仅是针对性问题,而是我的第一个问题,我对此并不熟悉。
  • 也许你可以看看stackoverflow.com/questions/818583/… 开始做一些事情。而且我猜您正在使用某种带有调试功能的 IDE 进行开发,因此您应该能够生成某种错误/异常消息,从而更轻松地为您提供帮助。

标签: c# .net printers


【解决方案1】:

经过大量研究,我让它在 x84 和 x64 平台上的 win 7、8、8.1、10 上运行(没有任何嵌入式驱动程序)。 代码如下:

public static void installPrinter(string printerName) //works on win 7,8,8.1,10 on both x84 and x64
       {
           //https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32-printui

           //  /if         Installs a printer by using an .inf file.
           //  /b[name]    Specifies the base printer name.
           //  /@[file]    Specifies a command-line argument file and directly inserts the text in that file into the command line.
           //  /f[file]    Species the Universal Naming Convention (UNC) path and name of the .inf file name or the output file name, depending on the task that you are performing. Use /F[file] to specify a dependent .inf file.
           //  /r[port]    Specifies the port name.
           //  /m[model]   Specifies the driver model name. (This value can be specified in the .inf file.)


           string arg;
           arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + @" /f C:\Windows\inf\ntprint.inf /r " + "\"" + "lpt1:" + "\"" + " /m " + "\"" + "Generic / Text Only" + "\""; 
           ProcessStartInfo p = new ProcessStartInfo();
           p.FileName = "rundll32.exe";
           p.Arguments = arg;
           p.WindowStyle = ProcessWindowStyle.Hidden;

           try
           {
               Process.Start(p);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.InnerException.ToString());
           }
       }

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 1970-01-01
    • 2016-04-16
    • 2013-07-12
    • 2020-09-08
    • 1970-01-01
    • 2016-06-16
    • 2017-08-13
    • 2011-04-24
    相关资源
    最近更新 更多