persisting text for windows forms app

I've made a small windows form app that has a few text boxes and other standard controls. I'd like to have the text that a user modifies in one session persist so it will appear next time the application is run. What is the best way to effect that kind of persistence?

I'm using c#, .net 2.0, & vs2005
LVL 3
OutsideTheBoxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

raja_mduCommented:
Use Serialize & deserialize methods

Create [Serializable] Class like below
// Serializable class
    [Serializable]
    public class Data
    {
        public string SomeString
        {
            get
            {
                return someString;
            }
            set
            {
                someString = value;
            }
        }
        private string someString;
    }
//////////////
And Implemnt the Following Code, it will serialize the data to xml file on closing event.
and deserialize the data from xml file on form load event;

i think it will help u



        // On Form load deserialize the data from xml and set the datas to the controls
        private void Form1_Load(object sender, EventArgs e)
        {
            persistData = new Data();
            
            try
            {
                TextReader reader = new StreamReader("Data.xml");
                XmlSerializer x = new XmlSerializer(persistData.GetType());
                persistData = (Data)x.Deserialize(reader);
                reader.Close();
                textBox1.Text = persistData.SomeString;
            }
            catch (Exception ex)
            {
 
            }
        }
 
        //On Closing the Form Wrie Serialize the data to xml File
        private void OnExit_Click(object sender, EventArgs e)
        {
            //TextBox Control
            persistData.SomeString = textBox1.Text;
 
            //Serialize data to xml
            TextWriter writer = new StreamWriter("Data.xml");
            XmlSerializer x = new XmlSerializer(persistData.GetType());
            x.Serialize(writer, persistData);
            writer.Close();
            this.Close();
        }

Open in new window

0
OutsideTheBoxAuthor Commented:
OK, now for more than one control, would I add another field/property to the data class?

    public class Data
    {
        public string SomeString
        {
            get
            {
                return someString;
            }
            set
            {
                someString = value;
            }
        }
        private string someString;

        public string SomeOtherString
        {
            get
            {
                return someOtherString;
            }
            set
            {
                someOtherString = value;
            }
        }
        private string someOtherString;
    }

then do this?
textBox2.Text = persistData.SomeOtherString;

Also, do I need to initially create an xml file and its schema or if I add a blank xml file to my project will it just automatically populate its data properly?
0
raja_mduCommented:
1. Yes u r right. u have to create one public property.
2. No need to create blank xml file, Code will generate automatically in current working path.
3. if current working path contain blank xml file, u will have some problem to serialize the data. so ple refactor the try catch block ( close the reader instance properly in catch block)
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

raja_mduCommented:
Better u can serialize the data to binary file. No one can edit this file
// Serialize
FileStream fs = new FileStream("Data.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, persistData);
fs.Close();

// DeSerialize
FileStream fs = new FileStream("Data.dat", FileMode.Open);
BinaryFormatter formatter = new BinaryFormatter();
persistData = (Data)formatter.Deserialize(fs);
fs.close();
0
J1H1W1Commented:
The capability you seek is built into the Visual Studio as Application Settings which uses the Settings file in your project properties.
"Application settings enable you to store and retrieve property settings and other information for your application dynamically. They also enable you to maintain custom application and user preferences on the client computer."
0
OutsideTheBoxAuthor Commented:
J1H1W1,

Could you show some basic demo code to utilize the settings feature?
0
J1H1W1Commented:
If you have a textbox on your form for which you want to persist the text, add a TB1Text variable to your settings file.  The code to load and save the text for the textbox would be similar to this:
public partial class Form1 : Form
{
  Properties.Settings MySettings = new Properties.Settings();
  public Form1()
  {
    InitializeComponent();
    textBox1.Text = MySettings.TB1Text;
  }
  private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  {
    MySettings.TB1Text = textBox1.Text;
    MySettings.Save();
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OutsideTheBoxAuthor Commented:
J1H1W1,

works perfect!

raja mdu,

yours also works and thanks for answering quickly. However, I do like J1H1W1's solution best.

Thanks

Steve
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.