I am trying to make a simple WindowsForm application that includes about 35 text boxes whos data I want saved at the press of a button into a text file so that I can load the data back into the text boxes when the program starts up. I am having 3 major problems.
- figuring out the best way to write the information to a text file
- how to specify the path of the text file if it is in the application root directory
- how to read from the file at startup (I believe this would happen in
Form1_Load
)
Here is the code I have been fiddling with that hasn't worked at all. Each textbox should have it's own line in the text file that never changes.
private void btnSaveCommands_Click(object sender, EventArgs e)
{
string path = System.IO.Path.GetDirectoryName("commands");
var lines = File.ReadAllLines("commands.txt");
lines[1] = commandTextBox1.Text;
lines[2] = commandTextBox2.Text;
lines[3] = commandTextBox3.Text;
lines[4] = commandTextBox4.Text;
lines[5] = commandTextBox5.Text;
etc..... (35 times)
File.WriteAllLines("commands.txt", lines);
}
for loading the text I would do something like....
private void Form1_Load(object sender, EventArgs e)
{
string path = System.IO.Path.GetDirectoryName("commands");
var lines = File.ReadAllLines("commands.txt");
commandTextBox1.Text = lines[1];
commandTextBox2.Text = lines[2];
etc....
}
I'm not sure if a text file is even the best way to do this kind of thing so if there are any better or easier options please mention them.
I have done some more research and found that a registry might work?