Click here to Skip to main content
Click here to Skip to main content

Serialize Printer Settings

By , 30 Sep 2003
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGet and Set Printer Settings in Windows 7memberdhanaz15 Jan '13 - 20:12 
Questionquestion regarding printer settingmembersandeepsokhal30 Aug '12 - 0:54 
QuestionIs there way to print MS Word doc with serialized settings?memberseemkot16 Jun '09 - 6:31 
GeneralProblem when running on Chinese Windows XPmemberlasselu18 Oct '07 - 3:03 
GeneralPrevious bug alertmemberTDWiseley4 Jun '06 - 17:19 
GeneralThanks!membercirrus198114 Nov '05 - 13:15 
GeneralGreat jobmemberTDWiseley20 Oct '05 - 12:34 
GeneralI think I found a mistakememberStephenKearney11 Apr '05 - 12:59 
GeneralRe: I think I found a mistakememberdhanaz15 Jan '13 - 19:49 
GeneralGreatmemberSeado2323324 Apr '05 - 6:37 
GeneralWorks greate EXCEPT....membervinnie88119 Feb '05 - 12:49 
GeneralThanks!memberJoão Paulo Figueira27 Jan '05 - 7:36 

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

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