Click here to Skip to main content
15,889,595 members
Articles / Programming Languages / C#
Article

Serialize Printer Settings

Rate me:
Please Sign up or sign in to vote.
4.74/5 (13 votes)
30 Sep 20032 min read 81.5K   2.5K   36   12
An article on how to serialize PrinterSettings.

Image 1

Introduction

It's very often to store user defined printer settings to disk, to get it later back from the disk. In the framework there is a class called PrinterSettings to manipulate the settings which are chosen by the printer dialog. In the framework 1.1 there is no way to serialize the PrinterSettings and store it to the disk. I think it's a bug in the framework?

Background

In some newsgroups there is always the same question, how can I store PrinterSettings in .NET. Now I tried different ways to do that, but I hope the best one is presented here. Its a mix of managed and unmanaged code. If you try to serialize the PrinterSettings object in the framework there appears always a serialization exception.

Using the code

The main class is clsPrinterSettings. In that class we will do the handling to serialize the class and the object.

There are three methods and two structures. The first method is

C#
public 
void setPrinterSettings(PrinterSettings settings)
, this method stores the settings object in the class. After you choose that method, you can serialize the clsPrinterSettings with normal .NET mechanisms. If you want to get back the settings next time, first you have to de-serialize the clsPrinterSettings and call the second method public PrinterSettings getPrinterSettings(), you get back the restored PrinterSettings object. The third method is private, because it's used to initialize a new PrinterSettings object in the clsPrinterSettings. That object is declared in the class as NonSerialized(). If you don't do that, you get a serialization exception. The two structures are to store the Hdevmodes and the Hdevnames in the structures.

Using the clsPrinterSettings

  1. Declaration of the classes is as shown below. The clsPrinterSettings is responsible to serialize the settings of the PrtSettings object.
    //Declarations 
    private clsPrinterSettings printSet = new clsPrinterSettings();
    private BinaryFormatter serializer = new  BinaryFormatter();
    private PrintDialog printer = new PrintDialog();
    private PrinterSettings PrtSettings = new  PrinterSettings();
  2. Check if a serialized file exists. If exists, de-serialize it and assign it to the PrtSettings object.
    //If printersettings exists then read it from the file
    if (File.Exists(sFilePrinterSet)){
    
        //Deserialise and assigning
        Stream Stream2 = File.Open(sFilePrinterSet, FileMode.Open);
        printSet = (clsPrinterSettings) serializer.Deserialize(Stream2);
        Stream2.Close();
        PrtSettings = printSet.getPrinterSettings();
    }
    //If printersettings not exists
    else{
        PrtSettings.PrinterName = txtSelPrinter.Text;
    }
  3. Assign the PrtSettings object to the Printer.PrinterSettings property and show the dialog.
    // Filling the PrinterDialog with the printer settings
    printer.PrinterSettings = PrtSettings;
    printer.ShowDialog();
  4. Serialize the printSet object after you assigned the new settings
    //assign the printersettings to the serialising class 
    printSet.setPrinterSettings(PrtSettings); 
     //serialise the serializing class 
    Stream Stream1 = File.Open(sFilePrinterSet, FileMode.Create);
    serializer.Serialize(Stream1,printSet);
    Stream1.Close();

History

  • 2003-09-28 Version 1.0

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGet and Set Printer Settings in Windows 7 Pin
dhanaz15-Jan-13 20:12
dhanaz15-Jan-13 20:12 
Questionquestion regarding printer setting Pin
sandeepsokhal30-Aug-12 0:54
sandeepsokhal30-Aug-12 0:54 
QuestionIs there way to print MS Word doc with serialized settings? Pin
somekot16-Jun-09 6:31
somekot16-Jun-09 6:31 
GeneralProblem when running on Chinese Windows XP Pin
lasselu18-Oct-07 3:03
lasselu18-Oct-07 3:03 
GeneralPrevious bug alert Pin
TDWiseley4-Jun-06 17:19
TDWiseley4-Jun-06 17:19 
The fix mentioned by StephenKearney is necessary. Without the bug, seems to run fine in XP, but crashes when running in 2003 server. With the bug fix, also runs in 2003 server.
GeneralThanks! Pin
cirrus198114-Nov-05 13:15
cirrus198114-Nov-05 13:15 
GeneralGreat job Pin
TDWiseley20-Oct-05 12:34
TDWiseley20-Oct-05 12:34 
GeneralI think I found a mistake Pin
StephenKearney11-Apr-05 12:59
StephenKearney11-Apr-05 12:59 
GeneralRe: I think I found a mistake Pin
dhanaz15-Jan-13 19:49
dhanaz15-Jan-13 19:49 
GeneralGreat Pin
Alen Oblak4-Apr-05 6:37
Alen Oblak4-Apr-05 6:37 
GeneralWorks greate EXCEPT.... Pin
vinnie88119-Feb-05 12:49
vinnie88119-Feb-05 12:49 
GeneralThanks! Pin
João Paulo Figueira27-Jan-05 7:36
professionalJoão Paulo Figueira27-Jan-05 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.