setting default printer in code (C#)

pmarius

New member
Joined
Feb 27, 2004
Messages
3
Hi everybody!
-
Does anyone have any idea how could I set the default printer from my C# code ?
--
I would like to reach the same result as I would set the default printer from Control Panel/Printer !
--
What classes should I use?
--
Thanks in advance for replies
 
I use the following to set the default printer on a win2k machine
Code:
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;
	}

}
 
Thank you for your early reply ! Thank you ver ymuch
But I still need your help !
--
you use : using mshtml !!!!!! my question is what dll do I have to import(add references) . In which dll do I find this namespace ?
--

You wrote
internal RegistryPrinter(string name, string value)
{
this.name = name;
this.value = value;
}

I can give my printers name but its quite difficult to get the printers registry(value). Could you show me how did you manage to call this RegistryPrinter class !
--
Tnaks in advance
 
you can take out the line that says include mshtml
I dont know how it got there (probably copy/paste) but its not needed for that class. Its from the ms html object library, a com object. I use it for html parsing.

RegistryPrinter.InstalledPrinters returns an array of RegistryPrinter objects. Each RegistryPrinter object in the array represents a printer thats available in the users controlPanel->Printer. When u find the printer u want to set as the default, invoke RegistryPrinter.SetAsDefaultPrinter. Each RegistryPrinter also has a Name and Value property. The Value property is the registry value and the Name property is the name of the printer, as it is displayed in ControlPanel->Printers.
 
Ohh my God Im so stupid, I could have noticed that before (to use RegistryPrinter.InstalledPrinters).
--
Imagine that I used PrinterSettings.InstalledPrinters where I got only the name and when I instantiated an object from RegistryPrinters class I didnt know what parameters to give.Thats why I had difficulty in how to call from outside, to get a registry value !
--
Your code is marvelous and now it works !!!!!!!!!!!!!!
You almost saved my "life" because I had been working on it for 2 days and till you gave the answer I didnt manage with it !
-
Thank you very much !
Bye
Pop Marius from Hungary
 
Back
Top