Let's say there's a Textbox in my app and I'm getting the username from the Textbox. I'm saving the username to a string. But when I relaunch the app, the string will be null. How can I save user data so that I will get it next time my app starts?
I know File reading & writing methods but I'm looking for a way that will not involve file reading & writing.
I'd use project settings (Settings Page, Project Designer). Once a setting is created you can set from the GUI like this:
Properties.Settings.Default.FirstUserSetting = "abc";
When you close the main form (there is an event for this) you can persist all settings like this:
Properties.Settings.Default.Save();
Ultimately a file in the user's profile is used to store the data, but you don't have to deal with it directly.
If you need to save a list of, for example strings, you can do this:
private void Form1_Load(object sender, EventArgs e)
{
string[] someList = Properties.Settings.Default.myList.Split('|');
listBox1.Items.AddRange(someList);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string[] someList = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
someList[i] = (string)listBox1.Items[i];
}
Properties.Settings.Default.myList = string.Join("|", someList);
Properties.Settings.Default.Save();
}