简体   繁体   中英

i want to display text from textfile in text box . how can i do this .. in C#

i want to display text from textfile in text box . how can i do this .. in C# Actually im making text to speech converter in C# .. SO i want to open text file and want show text of that file in my textbox .. here is my code

    private void button2_Click(object sender, EventArgs e)
    {
        OpenFileDialog O = new OpenFileDialog();
        O.ShowDialog();
        Loadfile(O.FileName);

    }
    private void Loadfile(string filename)
    {
        TextRange range;
        FileStream fStream;
        if (File.Exists(fileName))
        {
            range = new TextRange(textBox1.Text.TrimStart, textBox1.Text.TrimEnd);
            fStream = new FileStream(filename, FileMode.Open);
            range.Load(fStream, DataFormats.Text);
            fStream.Close();
        }


    }

i got error in textBox1.Text.TrimStart, textBox1.Text.TrimEnd .. i dont want to use Richtextbox because .. for that i have to use . Document property of richtextbox cz 4 tht i bound to use WPF ... (richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd)

Please Help me on this

Cheers ! Wahib Idris

Any help will be appreciated .. Thanx in advance

Please Help

This should work:

    private void Loadfile(string filename)
    {
        if (File.Exists(fileName))
        {
            textBox1.Text = File.ReadAllText(filename);
        }
    }
var fileText = File.ReadAllText(filePath);
textBox.Text = fileText;

You can load content of file to string simply this way:

private string Loadfile(string filePath)
{
  string text = String.Empty;

  if (File.Exists(filePath))
  {
    StreamReader streamReader = new StreamReader(filePath);
    text = streamReader.ReadToEnd();
    streamReader.Close();
  }

  return text;
}

The easiest way:

if (File.Exists(filePathString))
    yourTextBox.Text = File.ReadAllText(filePathString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM