posts - 82, comments - 0, trackbacks - 0

My Links

News

Archives

Post Categories

C# Download

Facebook

Informativa cookie

Linkedin

Microsoft

Msdn Gallery

Twitter

VisualBasic

Wiki Tecknet

Set the default printer with WMI

Through this example will be shown how to use WMI (Windows management instrumentation) to select and set the default printer from those available on your operating system.

To try this example requires the netFramework4.0

WMI (Windows Management Instrumentation) is a component of the operating system,
therefore not be downloaded separately, being already included,
that contains a set of tools for managing and configuring the operating system.
Using WMI, you can view and change the default printer
"like this", or see the information machine hardware
from the motherboard to the processor to the CPU until the installed cards and more.
One of the famous programs that interface with WMI and PowerShell.
As we shall see in this example, the classes made ​​available to programmatically
are. NET framework and are fairly easy to use.
Also do not forget to add reference to System.Management to
make use of this interesting technology, more information about WMI
available on Msdn Library. I report the sample code available in VisualBasic NET and C #. The examples were created using WPF (Windows Presentation Foundation), WPF also found several examples of Msdn Library.
 

// Call the dll Framework 4.0
using System;
using System.Windows;
using System.Management;

// Class Printer_Selection
namespace Printer_Selection
{
    /// <summary>
    /// Logic of Interaction for MainWindow.xaml
    /// </ Summary>
    public partial class MainWindow: Window
    {
        // Declaring a new instance of the ManagementObjectSearcher class
        ManagementObjectSearcher search = new ManagementObjectSearcher ();
        // Declaration ManagementObjectCollection class represents a diverse set of management objects retrieved through wmi
        ManagementObjectCollection results;
        // I define a two-element array of type Object
        Object [] args = new Object [1];

        // Constructor of MainWindow Class
        public MainWindow ()
        {
            // InitializeComponent Method
            InitializeComponent ();
        }

        // Loaded event of the MainWindow class
        private void Window_Loaded (object sender, RoutedEventArgs e)
        {
            // Test
            try
            {
                /* Check the variable search an instance of the ManagementObjectSearcher class
                 * We need to specify the query as a parameter, in this case
                 * Using select * from Win32_Printer will be searched all available printers and installed in
                 * Operating system.
                 */
                search = new ManagementObjectSearcher ("select * from Win32_Printer");

                /* The Get method calls belonging to class ManagementObjectSearcher
                 * The query we specified earlier in order to enhance the
                 * Variable results of type ManagementObjectCollection with the whole
                 Resulting *.
                 */
                results = search.Get();

                /* Perform a ForEach loop variable by assigning the printer
                 * All sets of type ManagementObject collection of variable
                 * Results
                 */
                foreach (ManagementObject printer in results)
                {
                    /* We value through the property Intems and the Add method of the ComboBox
                     * CbxAvialiblePrinters, which display all the printers
                     * Available in its interior, giving the possibility to select them.
                     */
                    cbxAvialiblePrinters.Items.Add (printer ["Name"]);
                }
            }

            /* We also run using a try / catch Exception class and a possible
             * Exception at runtime.
             */
            catch (Exception ex)
            {
                // The user by displaying a messagebox
                MessageBox.Show (ex.Message.ToString ());
            }
        }

        // Click event of Button btnSelect
        private void btnSelect_Click (object sender, RoutedEventArgs e)
        {
            // Test
            try
            {
                /* Perform a ForEach loop variable by assigning the printer
                * All sets of type ManagementObject collection of variable
                * Results
                */
                foreach (ManagementObject print in results)
                {
                    /* Verify that the value by using its Name
                     * Of the variable print is equal to the value of properties
                     * Text of the ComboBox cbxAvialiblePrinters
                     */
                    if (print ["Name"]. Equals (cbxAvialiblePrinters.Text))
                    {
                        /* We call the method InvokeMethod
                         * Which invokes the method on the past
                         * As an argument in this case SetDefaultPrinter,
                         * That will set as the default printer that the user
                         * Selected from the ComboBox control cbxAvialiblePrinters.
                         */
                        print.InvokeMethod ("SetDefaultPrinter", arg);

                        /* We display also using a TextBlock tbkPrinterSelect
                         * The name of the currently selected printer and set as
                         * Default printer.
                         */
                        tbkPrinterSelect.Text = cbxAvialiblePrinters.Text;
                        break;
                    }

                    /* Add a check if there has been selected
                     * No printer by the user using the SelectedIndex property.
                     * This property returns an integer which indicate which items the user has
                     * Selected in the non-selection will return a negative value -1.
                     */
                    else if (cbxAvialiblePrinters.SelectedIndex.Equals (-1))
                    {
                        /* Start by displaying a message which ask you to select a printer from
                         * ComboBox cbxAvialiblePrinters
                         */
                        MessageBox.Show ("Select a printer from the drop-down box", Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                        break;
                    }
                }
            }

            /* We also run using a try / catch Exception class and a possible
             * Exception at runtime.
             */
            catch (Exception ex)
            {
                 // We display screen user a messagebox
                MessageBox.Show (ex.Message.ToString ());
            }
        }
    }
}

 

The example is available at this address http://code.msdn.microsoft.com/Impostare-la-stampante-689f8b80

Carmelo La Monica.

Print | posted on domenica 17 giugno 2012 14:03 | Filed Under [ Articles C# in English ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 3 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET