Difference between revisions of "Selecting a text file in C"

From MyWiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<source lang="c#">
+
<source lang="csharp">
 
OpenFileDialog dialog = new OpenFileDialog();
 
OpenFileDialog dialog = new OpenFileDialog();
 
           dialog.Filter =
 
           dialog.Filter =
Line 10: Line 10:
 
                         richTextBox1.Text= System.IO.File.ReadAllText (fname);
 
                         richTextBox1.Text= System.IO.File.ReadAllText (fname);
  
           }
+
           }#
 
+
 
+
 
+
 
</source>
 
</source>
 +
Some notes<br>
 +
dialog.filename gives the file including path<br>
 +
dialog.SafeFilename gives only the filename<br>

Latest revision as of 12:57, 25 October 2018

OpenFileDialog dialog = new OpenFileDialog();
           dialog.Filter =
              "txt files (*.txt)|*.txt|All files (*.*)|*.*";
           dialog.InitialDirectory = "C:\\";
           dialog.Title = "Select a text file";
           if (dialog.ShowDialog() == DialogResult.OK)
           {
               string fname = dialog.FileName;
                         richTextBox1.Text= System.IO.File.ReadAllText (fname);
 
           }#

Some notes
dialog.filename gives the file including path
dialog.SafeFilename gives only the filename