Advertisement
C0BRA

Problem

Jul 4th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. using System.Diagnostics;
  6. using System.Drawing.Printing;
  7.  
  8. using Microsoft.Win32;
  9.  
  10. namespace DefaultPrinterFix
  11. {
  12.     class Program
  13.     {
  14.         public class ToDelete
  15.         {
  16.             public string valname;
  17.             public RegistryKey node;
  18.         }
  19.  
  20.         static void Main(string[] args)
  21.         {
  22.             Dictionary<string, bool> Printers = new Dictionary<string, bool>();
  23.  
  24.             foreach (string printer in PrinterSettings.InstalledPrinters)
  25.             {
  26.                 Printers[printer.ToLower()] = true;
  27.                 Console.WriteLine("Found printer \"{0}\"", printer);
  28.             }
  29.  
  30.             Console.Write("\n");
  31.  
  32.             Console.WriteLine("Searching for obsolete printers...");
  33.  
  34.             LinkedList<ToDelete> Obsolete = new LinkedList<ToDelete>();
  35.             RegistryKey rootnode;
  36.  
  37.             // Locate first set...
  38.             rootnode = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices", true);
  39.             Debug.Assert(rootnode != null, "Could not find the entry for \"Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices\"");
  40.  
  41.             foreach (string name in rootnode.GetValueNames()) // These are values
  42.             {
  43.                 if (!Printers.ContainsKey(name.ToLower()))
  44.                 {
  45.                     Console.WriteLine("Found obsolete printer refrence to {0}", name);
  46.                    
  47.                     Obsolete.AddLast(new ToDelete()
  48.                     {
  49.                         valname = name,
  50.                         node = rootnode
  51.                     });
  52.                 }
  53.             }
  54.  
  55.             // Locate second set...
  56.             rootnode = Registry.CurrentUser.OpenSubKey("Printers\\Connections", true);
  57.             Debug.Assert(rootnode != null, "Could not find the entry for \"Printers\\Connections\"");
  58.  
  59.             foreach (string name in rootnode.GetSubKeyNames()) // This section has folders with binary data about the printer, so lets just remove the whole folder
  60.             {
  61.                 if (!Printers.ContainsKey(name.Replace(",", "\\").ToLower()))     /* For some reason, the back slashes are replaced with
  62.                                                                          commas; to fix this so we can check the printer is
  63.                                                                          valid we just replace the comma with a backslash.
  64.                                                                          ,,ddrl1,example -> \\ddrl1\example */
  65.                 {
  66.                     Obsolete.AddLast(new ToDelete()
  67.                     {
  68.                         valname = name,
  69.                         node = rootnode
  70.                     });
  71.                 }
  72.             }
  73.  
  74.             // Time to delete them...
  75.             if (Obsolete.Count == 0)
  76.                 Console.WriteLine("\nNo obsolete printers found!");
  77.             else
  78.             {
  79.                 Console.WriteLine("\nFound {0} enteries:\n", Obsolete.Count);
  80.                 foreach (ToDelete obj in Obsolete)
  81.                     Console.WriteLine("{0}\\{1}", obj.node, obj.valname);
  82.  
  83.                 Console.Write("\nAre you sure you want to delete these enteries? (y/n): ");
  84.  
  85.                 string line = Console.ReadLine();
  86.                 Console.Write("\n");
  87.  
  88.                 if (line == "y" || line == "Y")
  89.                 {
  90.                     foreach (ToDelete key in Obsolete)
  91.                     {
  92.                         try { key.node.DeleteValue(key.valname); }
  93.                         catch { }
  94.                         try { key.node.DeleteSubKeyTree(key.valname); }
  95.                         catch { }
  96.                         Console.WriteLine("Deleted \"{0}\\{1}\"", key.node, key.valname);
  97.                     }
  98.                    
  99.                 }
  100.                 else
  101.                     Console.WriteLine("Halting.");
  102.  
  103.             }
  104.  
  105.             Console.WriteLine("\nPush enter to exit.");
  106.             Console.ReadLine();
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement