none
[UWP]How can I get the Printer name List in a universal App? RRS feed

Answers

  • This works Perfectly when I want to show a dialog box to print. But How can I get the name of available printers without displaying the dialog box?

    Hi,

    As I tell before you should use Windows.Devices.Enumeration API. Following code get all connected (direct or via LAN) devices ad show it in popup 

    private void button_Click(object sender, RoutedEventArgs e)
            {
                var devicePicker = new DevicePicker();
                Rect rect = new Rect(100, 100, 200, 200);
                devicePicker.Show(rect);
            }

    but what about if we want get only some devices type for example printer, camera, etc.. then we should filtering collection inside devicePicker for that purpose there is two ways 

    The first

    useing devicePicker.Filter.SupportedDeviceClasses.Add(DeviceClass.All);  but here DeviceClass enum contain only following values

    public enum DeviceClass
        {
            All = 0,
            AudioCapture = 1,
            AudioRender = 2,
            PortableStorageDevice = 3,
            VideoCapture = 4,
            ImageScanner = 5,
            Location = 6
        }

    and as we see there isn't flag for printers therefore we should use All flag but this mean that we return to start point.

    And I want ask to people from the Microsoft UWP team who create this API why you doesn't added  flags for Printers and other common devices?

    The second

    Because of in DeviceClass doesn't exist flag for Printer we have to use Advanced Query Syntax (AQS) filter as selector for devicePicker.Filter.SupportedDeviceSelectors . But for AQS string we should know the device GUID and DeviceInformationKind for printers the  DeviceInformationKind is DeviceInformationKind.DeviceInterface which mean that in AQS string prefix is "System.Devices.InterfaceClassGuid"                                                                                                        

    and GUID is {0ecef634-6ef0-472a-8085-5ad023ecbccd}

    The full AQS string "System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\""

    And final code 

     private async void button_Click(object sender, RoutedEventArgs e)
            {
                var devicePicker = new DevicePicker();
                devicePicker.Filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");
    
                devicePicker.DevicePickerDismissed += DevicePicker_DevicePickerDismissed;
    
                devicePicker.DeviceSelected += DevicePicker_DeviceSelected;
    
                Rect rect = new Rect(100, 100, 200, 200);
    
                devicePicker.Show(rect);
            }
    
            private void DevicePicker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
            {
              
            }
    
            private void DevicePicker_DevicePickerDismissed(DevicePicker sender, object args)
            {
               
            }

    If you want know GUID and DeviceInformationKind  for some device then show devicePicker without any filters find that device in list and select it then in  DevicePicker_DeviceSelected handler DeviceSelectedEventArgs contains that  formation in SelectedDevice parameter, the GUID is the last block of Id propery  and DeviceInformationKind  is Kind property

    documentation doc1 , doc2 and doc3


    Make the community better together




    • Edited by Azat Tazayan Tuesday, August 23, 2016 1:25 PM
    • Marked as answer by SubashAdhi Wednesday, August 24, 2016 3:54 AM
    Tuesday, August 23, 2016 1:23 PM

All replies

  • Hi SubashAdhi,

    Welcome to the Developing Universal Windows apps forum!

    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools

    If you want print some document from UWP app you should use  PrintManager from Windows.Graphics.Printing namespace. Following code demonstrate how to open Print dialog from UWP c# 

    private async void button_Click_1(object sender, RoutedEventArgs e)
            {
                PrintManager printMan = PrintManager.GetForCurrentView();
                printMan.PrintTaskRequested += PrintTaskRequested;
                await PrintManager.ShowPrintUIAsync();
            }
    
            private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
            {
                PrintTask printTask = null;
                printTask = args.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequested =>
                {
                    // Print Task event handler is invoked when the print job is completed.
                    printTask.Completed += async (s, e) =>
                    {
                        // Notify the user when the print operation fails.
                        if (e.Completion == PrintTaskCompletion.Failed)
                        {
                          
                        }
                    };
                    PrintDocument printDocument = new PrintDocument();
                    IPrintDocumentSource printDocumentSource = printDocument.DocumentSource;
                    sourceRequested.SetSource(printDocumentSource);
                });
            }
     

    There is Microsoft official sample on GitHub Windows-universal-samples/Samples/Printing/  and  documentation 

    Print from your app  .

    If you want get only connected devices, the you should use API provided by Windows.Devices.Enumeration there is also  Microsoft official sample on GitHub Windows-universal-samples/Samples/DeviceEnumerationAndPairing/  and documentation Enumerate devices


    Make the community better together

    Friday, August 19, 2016 10:02 AM
  • Thank you Azat, I will try this asap and mark it as answer if it works :) 
    Monday, August 22, 2016 12:27 PM
  • This works Perfectly when I want to show a dialog box to print. But How can I get the name of available printers without displaying the dialog box?
    Monday, August 22, 2016 12:42 PM
  • This works Perfectly when I want to show a dialog box to print. But How can I get the name of available printers without displaying the dialog box?

    Hi,

    As I tell before you should use Windows.Devices.Enumeration API. Following code get all connected (direct or via LAN) devices ad show it in popup 

    private void button_Click(object sender, RoutedEventArgs e)
            {
                var devicePicker = new DevicePicker();
                Rect rect = new Rect(100, 100, 200, 200);
                devicePicker.Show(rect);
            }

    but what about if we want get only some devices type for example printer, camera, etc.. then we should filtering collection inside devicePicker for that purpose there is two ways 

    The first

    useing devicePicker.Filter.SupportedDeviceClasses.Add(DeviceClass.All);  but here DeviceClass enum contain only following values

    public enum DeviceClass
        {
            All = 0,
            AudioCapture = 1,
            AudioRender = 2,
            PortableStorageDevice = 3,
            VideoCapture = 4,
            ImageScanner = 5,
            Location = 6
        }

    and as we see there isn't flag for printers therefore we should use All flag but this mean that we return to start point.

    And I want ask to people from the Microsoft UWP team who create this API why you doesn't added  flags for Printers and other common devices?

    The second

    Because of in DeviceClass doesn't exist flag for Printer we have to use Advanced Query Syntax (AQS) filter as selector for devicePicker.Filter.SupportedDeviceSelectors . But for AQS string we should know the device GUID and DeviceInformationKind for printers the  DeviceInformationKind is DeviceInformationKind.DeviceInterface which mean that in AQS string prefix is "System.Devices.InterfaceClassGuid"                                                                                                        

    and GUID is {0ecef634-6ef0-472a-8085-5ad023ecbccd}

    The full AQS string "System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\""

    And final code 

     private async void button_Click(object sender, RoutedEventArgs e)
            {
                var devicePicker = new DevicePicker();
                devicePicker.Filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");
    
                devicePicker.DevicePickerDismissed += DevicePicker_DevicePickerDismissed;
    
                devicePicker.DeviceSelected += DevicePicker_DeviceSelected;
    
                Rect rect = new Rect(100, 100, 200, 200);
    
                devicePicker.Show(rect);
            }
    
            private void DevicePicker_DeviceSelected(DevicePicker sender, DeviceSelectedEventArgs args)
            {
              
            }
    
            private void DevicePicker_DevicePickerDismissed(DevicePicker sender, object args)
            {
               
            }

    If you want know GUID and DeviceInformationKind  for some device then show devicePicker without any filters find that device in list and select it then in  DevicePicker_DeviceSelected handler DeviceSelectedEventArgs contains that  formation in SelectedDevice parameter, the GUID is the last block of Id propery  and DeviceInformationKind  is Kind property

    documentation doc1 , doc2 and doc3


    Make the community better together




    • Edited by Azat Tazayan Tuesday, August 23, 2016 1:25 PM
    • Marked as answer by SubashAdhi Wednesday, August 24, 2016 3:54 AM
    Tuesday, August 23, 2016 1:23 PM
  • Thank you Azat, this works perfect. :)
    Wednesday, August 24, 2016 3:55 AM
  • may be it will work in UWP app

    var Printers = new DevicePicker();
    Printers .Filter.SupportedDeviceSelectors.Add("System.Devices.InterfaceClassGuid:=\"{0ecef634-6ef0-472a-8085-5ad023ecbccd}\"");
    Printers .Show(new Rect());

    Thursday, July 11, 2019 12:19 PM