Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Diagnostics;
- using System.Drawing.Printing;
- using Microsoft.Win32;
- namespace DefaultPrinterFix
- {
- class Program
- {
- public class ToDelete
- {
- public string valname;
- public RegistryKey node;
- }
- static void Main(string[] args)
- {
- Dictionary<string, bool> Printers = new Dictionary<string, bool>();
- foreach (string printer in PrinterSettings.InstalledPrinters)
- {
- Printers[printer.ToLower()] = true;
- Console.WriteLine("Found printer \"{0}\"", printer);
- }
- Console.Write("\n");
- Console.WriteLine("Searching for obsolete printers...");
- LinkedList<ToDelete> Obsolete = new LinkedList<ToDelete>();
- RegistryKey rootnode;
- // Locate first set...
- rootnode = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices", true);
- Debug.Assert(rootnode != null, "Could not find the entry for \"Software\\Microsoft\\Windows NT\\CurrentVersion\\Devices\"");
- foreach (string name in rootnode.GetValueNames()) // These are values
- {
- if (!Printers.ContainsKey(name.ToLower()))
- {
- Console.WriteLine("Found obsolete printer refrence to {0}", name);
- Obsolete.AddLast(new ToDelete()
- {
- valname = name,
- node = rootnode
- });
- }
- }
- // Locate second set...
- rootnode = Registry.CurrentUser.OpenSubKey("Printers\\Connections", true);
- Debug.Assert(rootnode != null, "Could not find the entry for \"Printers\\Connections\"");
- foreach (string name in rootnode.GetSubKeyNames()) // This section has folders with binary data about the printer, so lets just remove the whole folder
- {
- if (!Printers.ContainsKey(name.Replace(",", "\\").ToLower())) /* For some reason, the back slashes are replaced with
- commas; to fix this so we can check the printer is
- valid we just replace the comma with a backslash.
- ,,ddrl1,example -> \\ddrl1\example */
- {
- Obsolete.AddLast(new ToDelete()
- {
- valname = name,
- node = rootnode
- });
- }
- }
- // Time to delete them...
- if (Obsolete.Count == 0)
- Console.WriteLine("\nNo obsolete printers found!");
- else
- {
- Console.WriteLine("\nFound {0} enteries:\n", Obsolete.Count);
- foreach (ToDelete obj in Obsolete)
- Console.WriteLine("{0}\\{1}", obj.node, obj.valname);
- Console.Write("\nAre you sure you want to delete these enteries? (y/n): ");
- string line = Console.ReadLine();
- Console.Write("\n");
- if (line == "y" || line == "Y")
- {
- foreach (ToDelete key in Obsolete)
- {
- try { key.node.DeleteValue(key.valname); }
- catch { }
- try { key.node.DeleteSubKeyTree(key.valname); }
- catch { }
- Console.WriteLine("Deleted \"{0}\\{1}\"", key.node, key.valname);
- }
- }
- else
- Console.WriteLine("Halting.");
- }
- Console.WriteLine("\nPush enter to exit.");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement