I'm using this method to save my connection sentence into a text file:
MemoryStream ms = new MemoryStream();
byte[] memData = Encoding.UTF8.GetBytes("Data Source=" + textBox1.Text + ";" + Environment.NewLine + "Initial Catalog=" + textBox2.Text + ";" + Environment.NewLine + "User ID=" + textBox3.Text + ";" + Environment.NewLine + "password=" + textBox4.Text + ";");
ms.Write(memData, 0, memData.Length);
FileStream strm = new FileStream(@"C:\Users\Çağatay\Desktop\test.txt", FileMode.Create, FileAccess.Write);
ms.WriteTo(strm);
ms.Close();
strm.Close();
MessageBox.Show("Connection Saved");
Is there a method for reloading lines to textboxes back with streamreader?
I searched and found this:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Çağatay\Desktop\test.txt");
while ((line = file.ReadLine()) != null)
{
MessageBox.Show(line);
counter++;
}
file.Close();
But I need something like:
textbox1.text=streamreader.readline[0]
textbox2.text=streamreader.readline[1]
Can anyone help?
Any reason you wouldn't just load the whole file?
string[] lines = File.ReadAllLines(@"C:\Users\Çağatay\Desktop\test.txt");
Likewise your first piece of code would be considerably simpler with:
File.WriteAllLines(
@"C:\Users\Çağatay\Desktop\test.txt",
new[]
{
"Data Source=" + textBox1.Text + ";",
"Initial Catalog=" + textBox2.Text + ";",
"User ID=" + textBox3.Text + ";",
"password=" + textBox4.Text + ";"
});
Or with C# 6:
File.WriteAllLines(
@"C:\Users\Çağatay\Desktop\test.txt",
new[]
{
$"Data Source={textBox1.Text};",
$"Initial Catalog={textBox2.Text};",
$"User ID={textBox3.Text};",
$"password={textBox4.Text};"
});
See more on this question at Stackoverflow