Click here to Skip to main content
Licence 
First Posted 30 Sep 2003
Views 49,563
Downloads 1,386
Bookmarked 34 times

Serialize Printer Settings

By | 30 Sep 2003 | Article
An article on how to serialize PrinterSettings.

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

About the Author

haag

Web Developer

Germany Germany

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionIs there way to print MS Word doc with serialized settings? Pinmemberseemkot6:31 16 Jun '09  
GeneralProblem when running on Chinese Windows XP Pinmemberlasselu3:03 18 Oct '07  
GeneralPrevious bug alert PinmemberTDWiseley17:19 4 Jun '06  
GeneralThanks! Pinmembercirrus198113:15 14 Nov '05  
GeneralGreat job PinmemberTDWiseley12:34 20 Oct '05  
GeneralI think I found a mistake PinmemberStephenKearney12:59 11 Apr '05  
GeneralGreat PinmemberSeado2323326:37 4 Apr '05  
GeneralWorks greate EXCEPT.... Pinmembervinnie88112:49 19 Feb '05  
GeneralThanks! PinmemberJoão Paulo Figueira7:36 27 Jan '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 1 Oct 2003
Article Copyright 2003 by haag
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid