We help IT Professionals succeed at work.

ASP.NET: Install printer using VB.NET

asp_net2
asp_net2 asked
on
Hi,

I would like to be able to create a gridview that list all network printer and have a user select there printer and have it automatically install instead of doing it manually.
Comment
Watch Question

Sachintana DissanayakeSenior Web Developer
CERTIFIED EXPERT

Commented:
Use the following WMI query to retrieve printers in the network.
I am not sure whether this is working inside the web application.

System.Management.ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");

If this doesn't work for network printers, please follow the link below, you might need to convert this to .net code.
http://msdn2.microsoft.com/en-us/library/aa394363.aspx

This will help you to find any active directory as SQL queries, that't interesting.
http://www.thescripts.com/forum/thread508386.html

To add printer follow these reference.
http://msdn2.microsoft.com/en-us/library/aa384769(VS.85).aspx

Author

Commented:
sachintana,

I appreciate the help but not what I'm looking for. I'm looking for a way to click a link on a web form and have it install the printer from the link I click on.

Example:
HP LaserJet 4200 would be the link and when I click that link it would automaticlly install the printer as default with the appropriate IP address.
Sachintana DissanayakeSenior Web Developer
CERTIFIED EXPERT

Commented:
So you can get all the printers installed in your machine using .net code.
      http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printersettings.installedprinters.aspx

This sample demonstrate, how to set the printer in the PrintDocument object (printDoc).
            printDoc.PrinterSettings.PrinterName = comboInstalledPrinters.Text

Author

Commented:
I tried to copy and past the code that Microsoft has but no dice!
Sachintana DissanayakeSenior Web Developer
CERTIFIED EXPERT

Commented:
Does PopulateInstalledPrintersCombo() function works for you and populate the dropdown list?

Is it problem of printing.
How does your PrintDocument is creating from the code.
Sachintana DissanayakeSenior Web Developer
CERTIFIED EXPERT

Commented:
Here's a sample of how to use PrintDocument...
http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

Please share you code to see how you are printing it in .NET.

Author

Commented:
sachintana,

What i'm looking for is a way for users in a network to click on a link in an asp.net gridview that displays the printer within there office. Once the user selects the appriate printer then it will install the printer without them doing anything.
Senior Web Developer
CERTIFIED EXPERT
Commented:
Are you clear with this code...

PopulateInstalledPrintersCombo() to populate network printers.

comboInstalledPrintersSelected() to print using selected printer in the dropdown.

   private void PopulateInstalledPrintersCombo()
    {
        // Add list of installed printers found to the combo box.
        // The pkInstalledPrinters string will be used to provide the display string.
        String pkInstalledPrinters;
        for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
        {
            pkInstalledPrinters = PrinterSettings.InstalledPrinters[i];
            comboInstalledPrinters.Items.Add(pkInstalledPrinters);
        }
    }
 
 
    private void comboInstalledPrintersSelected()
    {
        PrintDocument printDoc = new PrintDocument();
 
        // Set the printer to a printer in the combo box when the selection changes.
        if (comboInstalledPrinters.SelectedIndex != -1)
        {
            // The combo box's Text property returns the selected item's text, which is the printer name.
            printDoc.PrinterSettings.PrinterName = comboInstalledPrinters.Text;
            printDoc.Print();
        }
    }

Open in new window

Author

Commented:
sachintana,

How do I populate the printer now? I ran the code that you supplied and I added the namespace and a dropdown with the ID. The page runs fine but no printer where in the dropdown.
Sachintana DissanayakeSenior Web Developer
CERTIFIED EXPERT

Commented:
You have to first setup printers in windows (Control Panel).

Author

Commented:
sachintana,

I have 4 printers installed and neither are showing up in the dropdown. All, what i needed is a way for a user to click on the name of the printer of a location and have that printer that the user chooses install automatically without the user doing anything.
Sachintana DissanayakeSenior Web Developer
CERTIFIED EXPERT

Commented:
Did you test in your host machine? when code is running in the server, it will take Printers installed in the web server.

I don;t think you can get printer installed on user machine.