Advertisement
k3rama7

Load from .txt file into TextBox

Oct 18th, 2014
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. private void button_LoadSource_Click(object sender, EventArgs e)
  2.     {
  3.         Stream myStream = null;
  4.         OpenFileDialog openFileDialog1 = new OpenFileDialog();
  5.  
  6.         openFileDialog1.InitialDirectory = "c:\\";
  7.         openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  8.         openFileDialog1.FilterIndex = 2;
  9.         openFileDialog1.RestoreDirectory = true;
  10.  
  11.         if (openFileDialog1.ShowDialog() == DialogResult.OK)
  12.         {
  13.             try
  14.             {
  15.                 if ((myStream = openFileDialog1.OpenFile()) != null)
  16.                 {
  17.                     using (myStream)
  18.                     {
  19.                         textbox1.Text = File.ReadAllText(openFileDialog1.FileName);
  20.                     }
  21.                 }
  22.             }
  23.             catch (Exception ex)
  24.             {
  25.                 MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
  26.             }
  27.         }
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement