Hi this Raghu.I have to Save the State of Windows Form in Some File Format Such as .cfg by Button Click Event and Also when It was needed,it has to retrieve from the saved format and display the Saved Contents in C#.

Recommended Answers

All 3 Replies

XML is the way to go, baby! Store the form state in a separate object, then serialize that object using XmlSerializer. Separating form data from the form class makes this easy, and here is a class that makes the serialization even easier than it usually is. ;)

using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace Ed {
    public static partial class Xml {
        /// <summary>
        /// Restore an object created by the Serialize() method
        /// </summary>
        public static T Deserialize<T>(string xml) {
            XmlSerializer xs = new XmlSerializer(typeof(T));

            using (StringReader stream = new StringReader(xml))
                return (T)xs.Deserialize(stream);
        }

        /// <summary>
        /// Serialize to XML under the default namespace
        /// </summary>
        public static string Serialize<T>(T obj) {
            using (var writer = new StreamWriter(new MemoryStream(), Encoding.UTF8)) {
                var xs = new XmlSerializer(typeof(T));

                xs.Serialize(writer, obj);

                return Encoding.UTF8.GetString(((MemoryStream)writer.BaseStream).ToArray()).Trim();
            }
        }
    }
}

Thanks for your valuable reply.

Hi this Raghu.I have to Save the State of Windows Form in Some File Format Such as .cfg by Button Click Event and Also when It was needed,it has to retrieve from the saved format and display the Saved Contents in C#.

Take a look at stunning article about Serialization in .net (MSDN) - http://msdn.microsoft.com/en-us/library/ms973893.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.