سؤال

I am writing a winform application on .NET 2.0 that i need to open the popup that let user select a printer in LAN. It's exactly NOT a PrintDialog. I just need a popup same as the popup with title "Please select the network printer you want to use and click Select to connect to it" when you click on "Find Printer..." button of PrintDialog.

A popup like this

Anyone can help me to open this popup without opening a PrintDialog popup?

هل كانت مفيدة؟

المحلول

I don't know of a way to isolate that portion of the print dialog.

You probably need to use WMI, and write a function to accomplish what you want. I had to write something similar recently.

This function will populate all of the printers on a specified computer and list them in a combobox. I've set it to where it listed the printers as \\servername\\printername for my purposes, but feel free to edit it as you need.

I hope this helps.

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);
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top