Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

I use the following to set the default printer on a win2k machine

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

}

Posted

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

Posted

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.

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...