【问题标题】:Open The Standard "Find Network Printer" dialog打开标准的“查找网络打印机”对话框
【发布时间】:2013-12-26 18:29:38
【问题描述】:

我正在.NET 2.0 上编写一个 winform 应用程序,我需要打开让用户在 LAN 中选择打印机的弹出窗口。它完全不是 PrintDialog。当您单击 PrintDialog 的“查找打印机...”按钮时,我只需要一个与标题为“请选择要使用的网络打印机并单击选择以连接到它”的弹出窗口相同的弹出窗口。

类似this的弹出窗口

谁能帮我打开这个弹出窗口而不打开 PrintDialog 弹出窗口?

【问题讨论】:

  • 这是很多年前的事了,你能找到解决办法吗?,这正是我需要的。
  • 是啊,这么久。事实上,我不记得解决方案,但我已经通过 Alex 的以下建议解决了它。我不确定我是否仍然保留代码。如果我找到了,我会分享确切的代码。
  • 是的,如果可以的话,那将有很大帮助!

标签: c# winforms printing .net-2.0


【解决方案1】:

我不知道如何隔离打印对话框的那部分。

您可能需要使用 WMI,并编写一个函数来完成您想要的。我最近不得不写一些类似的东西。

此函数将填充指定计算机上的所有打印机并将它们列在组合框中。出于我的目的,我已将其设置为将打印机列为\\servername\\printername 的位置,但请随时根据需要对其进行编辑。

我希望这会有所帮助。

private void getPrinters(string domain, string username, string password, string server, ComboBox cmbBox)
    {
        progressBar.Maximum = 3;
        progressBar.Step = 1;
        ConnectionOptions objConnection = new ConnectionOptions();
        objConnection.Username = username;
        objConnection.Authority = "ntlmdomain:" + domain;
        objConnection.Password = password;
        ManagementScope scope = new ManagementScope(@"\\" + server + @"\root\cimv2", objConnection);
        try
        {
            scope.Connect();
        }
        catch (System.Runtime.InteropServices.COMException)
        {
            MessageBoxButtons msgButton = MessageBoxButtons.OK;
            MessageBoxIcon msgIcon = MessageBoxIcon.Error;
            string msgText = "Cannot connect to " + serverPrint + ".";
            string msgTitle = "Connection Error";
            DialogResult result = MessageBox.Show(msgText, msgTitle, msgButton, msgIcon);
            return;
        }
        catch (System.UnauthorizedAccessException)
        {
            MessageBoxButtons msgButton = MessageBoxButtons.OK;
            MessageBoxIcon msgIcon = MessageBoxIcon.Error;
            string msgText = "Bad Username or Password.";
            string msgTitle = "Authentication Error";
            DialogResult result = MessageBox.Show(msgText, msgTitle, msgButton, msgIcon);
            return;
        }
        SelectQuery selectQuery = new SelectQuery();
        selectQuery.QueryString = "Select * from win32_Printer";
        ManagementObjectSearcher MOS = new ManagementObjectSearcher(scope, selectQuery);
        ManagementObjectCollection MOC = MOS.Get();
        cmbBox.Items.Clear();
        foreach (ManagementObject mo in MOC)
        {
            string itemName = @"\\" + serverPrint + @"\" + mo["Name"].ToString();
            cmbBox.Items.Add(itemName);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 2012-07-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多