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.
   

Option Strict Off
Option Explicit On

Class MainWindow
    'Declaration instance of ManagementObjectSearcher class
    Dim search As System.Management.ManagementObjectSearcher
    Dim results As System.Management.ManagementObjectCollection

    'I call a two-element array of type Object
    Dim args (1) As Object

    'Statement class instance System.Management.ManagementObject
    Dim printer As System.Management.ManagementObject

    'Class Constructor MainWindow
    Private Sub MainWindow_Loaded (sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Test
        Try
            'Cheque to a class instance variable search ManagementObjectSearcher
            'With the query that we have to specify as a parameter, in this case
            'Select * from using Win32_Printer will be searched all available printers and installed in
            'Operating system.
            New System.Management.ManagementObjectSearcher search = ("select * from Win32_Printer")

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


            'We make a ForEach loop variable by assigning the printer
            'Type ManagementObject collection of all sets variable
            'Results
            For Each printer In results
                'We value Intems through the property and the Add method of the ComboBox
                'CbxAvialiblePrinters, which display all the printers
                'Available on the inside, giving the possibility to select them.
                cbxAvialiblePrinters.Items.Add (printer ("Name"))
            Next

            'We also run using a try / catch Exception class and a possible exception at runtime.
        Catch ex As Exception
            'The user by displaying a messagebox
            MessageBox.Show (ex.Message.ToString ())
        End Try
    End Sub

    'Click event of Button btnSelect
    Private Sub btnSelect_Click (sender As System.Object, e As System.Windows.RoutedEventArgs)

        'Test
        Try

            'We make a ForEach loop by assigning the variable prnt
            'Type ManagementObject collection of all sets variable
            'Results
            For Each printer In results
                'We check that the value using its Name
                'Of the variable print is equal to the value of properties
                'Text of the ComboBox cbxAvialiblePrinters
                If printer ("Name") = Then 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.
                    printer.InvokeMethod ("SetDefaultPrinter", args (0))
                    'Visualise also using a TextBlock tbkPrinterSelect
                    'The name of the currently selected printer and set as
                    'Default printer.
                    tbkPrinterSelect.Text = cbxAvialiblePrinters.Text
                    'We also 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
                    'Chosen in the non-selection will return a negative value -1.
                ElseIf (cbxAvialiblePrinters.SelectedIndex.Equals (-1)) Then

                    'We display a message asking where you select a printer from
                    'ComboBox cbxAvialiblePrinters
                    MessageBox.Show ("Select a printer from the drop-down box", Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation)
                End If
            Next

            'We also run using a try / catch Exception class and a possible exception at runtime.
        Catch ex As Exception
            'The user by displaying a messagebox
            MessageBox.Show (ex.Message.ToString ())
        End Try
    End Sub
End Class

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:08 | Filed Under [ Articles VB.NET in English ]

Feedback

No comments posted yet.

Post Comment

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

Powered by:
Powered By Subtext Powered By ASP.NET