using System;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using mshtml;
using Microsoft.Win32;
namespace HB.Stuff
{
/// <summary>
/// Summary description for RegistryPrinter.
/// </summary>
public class RegistryPrinter
{
/// <summary>
/// Creates a new instance of a <see cref="RegistryPrinter"/>.
/// </summary>
/// <param name="name">The name of the printer.</param>
/// <param name="value">The registry value of the printer.</param>
internal RegistryPrinter(string name, string value)
{
this.name = name;
this.value = value;
}
public static RegistryPrinter[] InstalledPrinters
{
get
{
string subkey = @"Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts";
RegistryKey key = Registry.CurrentUser.OpenSubKey(subkey, false);
string[] printerNames = key.GetValueNames();
RegistryPrinter[] printers = new RegistryPrinter[printerNames.Length];
for(int i=0; i < printerNames.Length; i++)
printers[i] = new RegistryPrinter(printerNames[i], (string) key.GetValue(printerNames[i]));
key.Close();
return printers;
}
}
/// <summary>
/// Gets the name of the printer.
/// </summary>
public string Name
{
get
{
return name;
}
set
{
string subkey = @"Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts";
RegistryKey key = Registry.CurrentUser.OpenSubKey(subkey, false);
key.SetValue(value, Value);
key.DeleteValue(name);
name = value;
key.Close();
}
}
/// <summary>
/// Gets the registry value of the printer.
/// </summary>
public string Value
{
get
{
return value;
}
}
/// <summary>
/// Set this printer as the default printer for the <see cref="Registry.CurrentUser"/>.
/// </summary>
public void SetAsDefaultPrinter()
{
Hashtable printers = new Hashtable();
string subkey = @"Software\Microsoft\Windows NT\CurrentVersion\Windows";
RegistryKey key = Registry.CurrentUser.OpenSubKey(subkey, true);
string[] subvalues = value.Split(,);
key.SetValue("Device", name + "," + subvalues[0] + "," + subvalues[1]);
key.Close();
}
public static RegistryPrinter GetDefaultPrinter()
{
string subkey = @"Software\Microsoft\Windows NT\CurrentVersion\Windows";
RegistryKey key = Registry.CurrentUser.OpenSubKey(subkey, false);
string defaultPrinter = ((string) key.GetValue("Device")).Split(,)[0];
key.Close();
foreach(RegistryPrinter printer in InstalledPrinters)
if(printer.Name == defaultPrinter)
return printer;
return null;
}
private string name;
private string value;
}
}