Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to save some settings for my app.
For bad luck there're also printer settings available ...
Saving the settings looks fine but when I load them again I ever get an exception null value here:
System.Drawing.Printing.PrinterSettings.set_PrintFileName(String value)

Is there a way to also serialize the printer settings?
My code I use for other settings is included how to expand to make it work with the printer settings?

C#
public class PrintDataSettings
{
    public PrintDataSettings()
    {
            DefaultPrinterSettings = new PrinterSettings();
    }
    public PrintDataSettings(PrintDataSettings printSettings)
    {
            DefaultPrinterSettings = printSettings.DefaultPrinterSettings;
    }

        [XmlElement("DefaultPrinterSettings")]
        public PrinterSettings DefaultPrinterSettings { get; set; }
}



Thanks
Harish.
Posted
Comments
Richard MacCutchan 30-Dec-13 9:26am    
Use you debugger to identify which item is a null value.
BillWoodruff 30-Dec-13 11:08am    
System.Drawing.Printing.PrinterSettings.set_PrintFileName(String value)

This code does not make sense: System.Drawing.Printing.PrinterSettings is a Class; there is no method in that Class like the one you show being used.

1 solution

Hello,

You have an exception during de-serialization in System.Drawing.Printing.PrinterSettings.set_PrintFileName(String value) because it is not allowed to set the property PrintFileName to an empty or null string, the setter of the property throws this exception.

I go round this issue by assigning a fake file name to the property, it is not a problem as it is unused if PrintTofile is False.

PrinterSettings prtSettings = new PrinterSettings();
prtSettings.PrintFileName = "does not matter, unused if PrintToFile == false";
						
//serialise
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(prtSettings.GetType());
using (System.IO.TextWriter txtWriter = new StreamWriter(@"c:\temp\printerSettings.xml"))
{
	xmlSerializer.Serialize(txtWriter,prtSettings);
}
			
//deserialise
using (FileStream fileStream = new FileStream(@"c:\temp\printerSettings.xml", FileMode.Open))
{
	object obj = xmlSerializer.Deserialize(fileStream);
	prtSettings = (PrinterSettings)obj;
}



Valery.
 
Share this answer
 
Comments
Harish Reddy K 4-Jan-14 0:28am    
Thanks Valery,

this solution worked for me....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900