本文介绍了如何序列化PrinterSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要为我的应用程序保存一些设置。

运气不好也有打印机设置可用。 ..

保存设置看起来很好但是当我再次加载它时,我在这里得到一个异常空值:

System.Drawing.Printing.PrinterSettings.set_PrintFileName(String value )



有没有办法同时序列化打印机设置?

我用于其他设置的代码包括如何扩展到make它适用于打印机设置?



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?

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

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







谢谢

Harish。




Thanks
Harish.

推荐答案

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。




Valery.


这篇关于如何序列化PrinterSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 01:57