General Topics include: .NET Framework
Note: Most posts can be categorized into one of the forums below, where experts on that topic will be able to help you find a solution to your problem faster and more efficiently.

Go Back  Xtreme .NET Talk > .NET > General > setting default printer in code (C#)


Reply
 
Thread Tools Display Modes
  #1  
Old 02-27-2004, 05:53 AM
pmarius pmarius is offline
Newcomer
 
Join Date: Feb 2004
Posts: 3
pmarius is on a distinguished road
Default setting default printer in code (C#)



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
Reply With Quote
  #2  
Old 02-27-2004, 07:15 AM
Merrion Merrion is offline
Junior Contributor

Preferred language:
VB.Net
 
Join Date: Sep 2001
Location: Dublin, Ireland
Posts: 265
Merrion is on a distinguished road
Default

You will need to use the Windows API as the SetPrinter functionality is not yet in the class library.
__________________
Printer Monitor for .NET? - see Merrion Computing Ltd for details
Reply With Quote
  #3  
Old 02-27-2004, 07:42 AM
HJB417's Avatar
HJB417 HJB417 is offline
Contributor

Preferred language:
c#, c++, j#
 
Join Date: Mar 2003
Location: Lowell, MA
Posts: 609
HJB417 is on a distinguished road
Default

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;
	}

}
Reply With Quote
  #4  
Old 02-27-2004, 08:33 AM
pmarius pmarius is offline
Newcomer
 
Join Date: Feb 2004
Posts: 3
pmarius is on a distinguished road
Default

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 printer's name but it's quite difficult to get the printer's registry(value). Could you show me how did you manage to call this RegistryPrinter class !
--
Tnaks in advance
Reply With Quote
  #5  
Old 02-27-2004, 10:24 PM
HJB417's Avatar
HJB417 HJB417 is offline
Contributor

Preferred language:
c#, c++, j#
 
Join Date: Mar 2003
Location: Lowell, MA
Posts: 609
HJB417 is on a distinguished road
Default

you can take out the line that says include mshtml
I don't know how it got there (probably copy/paste) but it's not needed for that class. It's 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 that's available in the user's 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.
Reply With Quote
  #6  
Old 02-28-2004, 02:41 AM
pmarius pmarius is offline
Newcomer
 
Join Date: Feb 2004
Posts: 3
pmarius is on a distinguished road
Default

Ohh my God I'm 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 didn't know what parameters to give.That's 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 didn't manage with it !
-
Thank you very much !
Bye
Pop Marius from Hungary
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing the Default Printer with .net SonicBoomAu General 1 10-31-2004 07:37 PM
Setting default values for a null field mike55 Database / XML / Reporting 4 10-19-2004 11:09 AM
Setting default time out value in .Net burak General 0 01-23-2004 09:41 AM
ComboBox default setting BlueOysterCult Windows Forms 2 10-20-2003 03:02 PM
Printing Report on Default Printer of Client machine ann ASP.NET 1 08-30-2003 12:12 AM

Advertisement:







Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe