To lazy to make a tutorial
-what you need
using System.IO; (top of code)
OpenFileDialog (renamed dialog)
TextBox (txtData)
Uses FileDialog to find the .txt file the user wants to open, saves the location in a string, StreamReader opens / reads all info in the .txt file.PHP Code:
txtData.Text = "";
string strFileLoc = "";
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.InitialDirectory = "C:"; dialog.Title = "Open";
if (dialog.ShowDialog() == DialogResult.OK)
{
strFileLoc = dialog.FileName;
try
{
using (StreamReader sr = new StreamReader(strFileLoc))
{
string line;
while ((line = sr.ReadLine()) != null)
{
txtData.Text += line + "\r\n";
}
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to open document. Maybe it doesn't exist or the data is corrupted?", "Error");
}
}
else if (strFileLoc == String.Empty)
{
return;
}
Results 1 to 1 of 1
- 18 Feb. 2010 02:02am #1
[C#.net] Open File via Dialog / print on textbox