Guest User

Untitled

a guest
May 28th, 2025
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Drawing.Printing;
  3. using System.Management;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         Console.WriteLine("Installed Printers and Their Details:\n");
  10.  
  11.         foreach (string printerName in PrinterSettings.InstalledPrinters)
  12.         {
  13.             Console.WriteLine($"Printer: {printerName}");
  14.            
  15.             string portName = GetPrinterPort(printerName);
  16.             Console.WriteLine($"  Port: {portName}");
  17.             Console.WriteLine($"  Type: {(IsVirtualPrinter(portName) ? "Virtual Printer" : "Physical Printer")}");
  18.  
  19.             PrintTrayDetails(printerName);
  20.             Console.WriteLine(new string('-', 50));
  21.         }
  22.     }
  23.  
  24.     static void PrintTrayDetails(string printerName)
  25.     {
  26.         try
  27.         {
  28.             PrinterSettings settings = new PrinterSettings();
  29.            
  30.             settings.PrinterName = printerName;
  31.  
  32.             if (!settings.IsValid)
  33.             {
  34.                 Console.WriteLine("  (Printer is not accessible)");
  35.                 return;
  36.             }
  37.  
  38.             PrinterSettings.PaperSourceCollection paperSources = settings.PaperSources;
  39.             Console.WriteLine($"  Number of Trays: {paperSources.Count}");
  40.  
  41.             foreach (PaperSource tray in paperSources)
  42.             {
  43.                 int capacity = GetTrayCapacity(printerName, tray.SourceName);
  44.                 Console.WriteLine($"  - Tray: {tray.SourceName} (Kind: {tray.Kind}) - Capacity: {(capacity > 0 ? capacity + " sheets" : "Unknown")}");
  45.             }
  46.          
  47.         }
  48.         catch (Exception ex)
  49.         {
  50.             Console.WriteLine($"  Error retrieving tray info: {ex.Message}");
  51.         }
  52.     }
  53.  
  54.     static string GetPrinterPort(string printerName)
  55.     {
  56.         try
  57.         {
  58.             string query = $"SELECT PortName FROM Win32_Printer WHERE Name = '{printerName.Replace("\\", "\\\\")}'";
  59.             using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
  60.             {
  61.                 foreach (ManagementObject printer in searcher.Get())
  62.                 {
  63.                     return printer["PortName"]?.ToString() ?? "Unknown";
  64.                 }
  65.             }
  66.         }
  67.         catch (Exception ex)
  68.         {
  69.             return $"Error: {ex.Message}";
  70.         }
  71.         return "Unknown";
  72.     }
  73.  
  74.     static int GetTrayCapacity(string printerName, string trayName)
  75.     {
  76.         try
  77.         {
  78.             string query = $"SELECT * FROM Win32_PrinterPaperTray WHERE PrinterName = '{printerName.Replace("\\", "\\\\")}'";
  79.             using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
  80.             {
  81.                 foreach (ManagementObject tray in searcher.Get())
  82.                 {
  83.                     if (tray["PaperTray"] != null && tray["PaperCapacity"] != null)
  84.                     {
  85.                         int capacity = Convert.ToInt32(tray["PaperCapacity"]);
  86.                         return capacity > 0 ? capacity : -1;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         catch
  92.         {
  93.             return -1; // Return -1 if information is unavailable
  94.         }
  95.         return -1;
  96.     }
  97.  
  98.     static bool IsVirtualPrinter(string portName)
  99.     {
  100.         if (string.IsNullOrEmpty(portName)) return false;
  101.  
  102.         string[] virtualPorts = { "nul", "PORTPROMPT", "FILE:" };
  103.         return Array.Exists(virtualPorts, p => portName.Equals(p, StringComparison.OrdinalIgnoreCase));
  104.     }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment