Reputation: 33
I want to copy the text from a .txt file into a richbox when the form loads. I don't want to open a dialog to choose the file, just open a specific file automatically
Stream sr;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((sr = openFileDialog1.OpenFile()) != null)
{
string strfilename = openFileDialog1.FileName;
string commandstext = File.ReadAllText(strfilename);
richTextBox2.Text=commandstext;
}
}
Upvotes: 0
Views: 89
Reputation: 1193
using (StreamReader sr = new StreamReader("YourFilePath.txt"))
{
String line = sr.ReadToEnd();
richTextBox2.Text=line;
}
Upvotes: 0
Reputation: 3024
You can also do as suggested in other 2 answers.
OR other faster
way as:
void LoadFileToRTB(string fileName, RichTextBox rtb)
{
rtb.LoadFile(File.OpenRead(fileName), RichTextBoxStreamType.PlainText); // second parameter you can change to fit for you
// or
rtb.LoadFile(fileName);
// or
rtb.LoadFile(fileName, RichTextBoxStreamType.PlainText); // second parameter you can change to fit for you
}
Upvotes: 2
Reputation: 3444
Stream sr;
string strfilename = "PATH TO FILE"; //Code should know the path already
string commandstext = File.ReadAllText(strfilename);
richTextBox2.Text=commandstext;
Upvotes: 0