OpenFile Dialog Box to select a file. For a wrong input (filename) it will show a error message.
private void btnOpenTextFile_Click(object sender, System.EventArgs e) { StreamReader ts = null; try { odlgTextFile.CheckFileExists = true;
// Check to ensure that the selected // path exists. Dialog box displays // a warning otherwise.
odlgTextFile.CheckPathExists = true;
odlgTextFile.DefaultExt = "txt";
// return the file referenced by a link? if // false, simply returns the selected link // file. if true, returns the file linked to // the LNK file.
odlgTextFile.DereferenceLinks = true;
// Just in VB6, use a set of pairs // of filters, separated with "|". Each // pair consists of a description|file spec. // Use a "|" between pairs. No need to put a // trailing "|". You can set the FilterIndex // property well, to select the default // filter. Amazingly, the first filter is // numbered 1 (! 0). The default is 1.
odlgTextFile.Filter = "Text files (*.txt)|*.txt|" + "All files|*.*";
odlgTextFile.Multiselect = false;
// Restore the original directory when done selecting // a file? if false, the current directory changes // to the directory in which you selected the file. // Set this to true to put the current folder back // where it was when you started. // The default is false.
odlgTextFile.RestoreDirectory = true;
// Show the Help button and Read-Only checkbox?
odlgTextFile.ShowHelp = true;
odlgTextFile.ShowReadOnly = false;
// Start out with the read-only check box checked? // This only make sense if ShowReadOnly is true. // .ReadOnlyChecked = false
odlgTextFile.Title = "Select a file to open";
// Only accept valid Win32 file names?
odlgTextFile.ValidateNames = true;
if (odlgTextFile.ShowDialog() == DialogResult.OK) { FileName = odlgTextFile.FileName; ts = new StreamReader(odlgTextFile.OpenFile()); txtFileContents.Text = ts.ReadToEnd(); } } catch (Exception exp) { MessageBox.Show(exp.Message, this.Text); } finally { if(ts != null) { ts.Close(); } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|