Reputation: 263
private void button1_Click(object sender, EventArgs e)
{
string accountsSettingsFile = Path.GetDirectoryName(Application.LocalUserAppDataPath)
+ "\\accounts" + "\\accounts.txt";
if (!File.Exists(accountsSettingsFile))
File.Create(accountsSettingsFile);
System.IO.File.WriteAllText(accountsSettingsFile,);
}
I want to build a string right before writing the content to the text file in the button click event. Maybe StringBuilder
and to format it to contain all the data from some controls.
I have a textBox1
, textBox2
, textBox3
a checkBox
.
I want to get all this controls data and write it in lines to the text file.
For example if in textBox1
I have "hello"
and in textBox2
"world"
and in textBox3
"hi"
and the checkBox
is false
, then the text file content should be something like:
hello
world
hi
false
Upvotes: 0
Views: 52
Reputation: 6308
I would go with something like this, especially if your list of controls might get longer later on:
private void button1_Click(object sender, EventArgs e)
{
Form1.WriteToFile(textBox1, textBox2, textBox3, checkBox);
}
private static void WriteToFile(params Control[] controls)
{
string accountsSettingsFile = Path.GetDirectoryName(Application.LocalUserAppDataPath)
+ "\\accounts" + "\\accounts.txt";
List<string> lines = new List<string>(controls.Length);
foreach (var control in controls)
{
string value = Form1.GetValueFromControl(control);
//this will skip null entries, not sure you really
//want to do that, otherwise when you read this file back in
//you will have no idea which values represent which fields
if (value != null)
lines.Add(value);
}
//This will overwrite the file.
//If you want to append use File.AppendAllLines
File.WriteAllLines(accountsSettingsFile, lines);
}
private static string GetValueFromControl(Control control)
{
if (control is TextBox)
{
return ((TextBox)control).Text;
}
if (control is CheckBox)
{
return ((CheckBox)control).Checked.ToString();
}
return null;
}
Though, since you are using this for settings, writing raw values to a text file is a very brittle approach. Might I recommend using serialization? Though this requires that you reference Newtonsoft.Json (via nuget) in your project:
private void Button_Write_Click(object sender, EventArgs e)
{
AccountSettings settings = new AccountSettings();
settings.Setting1 = this.textBox1.Text;
settings.Setting2 = this.textBox2.Text;
settings.Setting3 = this.textBox3.Text;
settings.CheckboxValue = this.checkBox.Checked;
WriteJson(settings, SettingsFile);
}
private void Button_Read_Click(object sender, EventArgs e)
{
AccountSettings settings = ReadJson<AccountSettings>(SettingsFile);
this.textBox1.Text = settings.Setting1;
this.textBox2.Text = settings.Setting2;
this.textBox3.Text = settings.Setting3;
this.checkBox.Checked = settings.CheckboxValue;
}
private static string SettingsFile
{
get
{
return Path.GetDirectoryName(Application.LocalUserAppDataPath)
+ "\\accounts" + "\\accounts.txt";
}
}
private static void WriteJson(Object obj, string path)
{
var ser = new JsonSerializer();
using (var file = File.CreateText(path))
using (var writer = new JsonTextWriter(file))
{
ser.Serialize(writer, obj);
}
}
private static T ReadJson<T>(string path)
where T: new()
{
if (!File.Exists(path))
return new T();
var ser = new JsonSerializer();
using (var file = File.OpenText(path))
using (var reader = new JsonTextReader(file))
{
return ser.Deserialize<T>(reader);
}
}
private class AccountSettings
{
public string Setting1 { get; set; }
public string Setting2 { get; set; }
public string Setting3 { get; set; }
public bool CheckboxValue { get; set; }
}
}
This gives you a strongly typed AccountSettings
object that can be written and read in a very concrete and repeatable manner.
Upvotes: 3
Reputation: 3204
private void button1_Click(object sender, EventArgs e)
{
string accountsSettingsFile = Path.GetDirectoryName(Application.LocalUserAppDataPath)
+ "\\accounts" + "\\accounts.txt";
if (!File.Exists(accountsSettingsFile))
File.Create(accountsSettingsFile);
//New Code
StringBuilder accountText = new StringBuilder();
accountText.AppendLine(textbox1.Text);
accountText.AppendLine(textbox2.Text);
accountText.AppendLine(textbox3.Text);
accountText.AppendLine(checkbox.Checked.ToString().ToLowerInvariant());
//Above line assumes you want a trailing newline
System.IO.File.WriteAllText(accountsSettingsFile, accountText.toString());
}
Of course, this is pretty boring and isn't particularly scaleable, if you wanted to generate a dynamic page with multiple accounts being entered at once, you could use Page.FindControl
to loop through your text boxes and append all of them to the file.
Upvotes: 1
Reputation: 26
var checked = checkBox.Checked ? "true" : "false";
var textToBeSaved = string.Format("{0}\n{1}\n{2}\n{3}", textBox1.Text, textBox2.Text, textBox3.Text, checked)
Upvotes: 1