Sequential Input and Output using Text Files Text File I/O
Open and Save File Dialogs Using pre-built standard dialogs to select files to be opened and saved
Caveat These dialogs were designed to use in Windows GUI applications rather than console apps To make them work with console applications: You must add a reference to System.Windows.Forms (see next slide) Add a using statement for the System.Windows.Forms namespace Add the compiler attribute [STAThread] to the Main method in your console application
Adding a Reference
OpenFileDialog Multiform Applications November 10, 2018
OpenFileDialog Example Create dialog instance Caption for the dialog Filter: limits which files show in the dialog If the user clicked OK in the dialog, process the selected file When dialog first opens, initial directory is folder where app’s .exe is found Filter contains sequences of “description|*.ext” where “description” is a phrase that will be shown describing what is being shown and “*.ext” specifies which file extensions are to be shown Multiform Applications November 10, 2018
OpenFileDialog Example Caption Initial Directory Filter Only .txt files shown Dropdown filter ComboBox Expanded Multiform Applications November 10, 2018
SaveFileDialog Multiform Applications November 10, 2018
SaveFileDialog Example Create instance Set initial directory, caption, and filters Display dialog, and, if not cancelled by user, process the selected file Multiform Applications November 10, 2018
Designate a Relative Path to file
Folder Structure
SaveFileDialog Example Caption Initial Directory Only *.txt files shown Filter Multiform Applications November 10, 2018
Simple Text File I/O Simple reading and writing from/to a Text File Multiform Applications November 10, 2018
Text File I/O The StreamReader and StreamWriter classes are provided by .NET for simple text file I/O They must be opened before the first I/O operation and closed after the last I/O operation Failure to close a file may result in loss of data so it is important to close a file after its last I/O operation The OpenFileDialog and SaveFileDialog may be used to help to associate actual files with StreamReader and StreamWriter objects in a C# program Multiform Applications November 10, 2018
Opening the StreamReader StreamReader constructor takes a string FileName This shows opening a StreamReader object associated with an existing file selected with an OpenFileDialog. Note: this operation could fail for a variety of reasons, so it should be in a try/catch. Multiform Applications November 10, 2018
Closing the StreamReader Close the StreamReader – if we successfully opened it Note that this code is in the Finally block since we want to close the file even if an exception occurred in reading data from the file Multiform Applications November 10, 2018
Reading From a StreamReader Read one line of data from the StreamReader The rest of the while loop processes the data but is unrelated to reading from the StreamReader This file is comma-delimited, but that does not always apply in other cases While there is more data … Multiform Applications November 10, 2018
Putting it all together … Array of String Multiform Applications November 10, 2018
Opening a StreamWriter for Output File is an output only file File Mode – see next slide File name from the dialog In a try block FileMode.Create says if the file does not already exist, create it. If the file does exist, it will be erased and replaced by the new file. Multiform Applications November 10, 2018
FileMode Member name Description CreateNew Create Open OpenOrCreate Create a new file. This requires Write access. If the file already exists, an IOException is thrown. Create Create a new file. If the file already exists, it will be overwritten. Requires Write access. Open Open an existing file. A FileNotFoundException is thrown if the file does not exist. OpenOrCreate Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. Truncate Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes. Attempts to read from a file opened with Truncate cause an exception. Append Opens the file if it exists and seeks to the end of the file, or creates a new file.
FileAccess Member name Description Read Write ReadWrite Read access to the file. Data can be read from the file. Combine with Write for read/write access. Write Write access to the file. Data can be written to the file. Combine with Read for read/write access. ReadWrite Read and write access to the file. Data can be written to and read from the file.
Writing data to a StreamWriter Write one line of data to the output file; note that fields in the data are “comma-delimited” as a result of this WriteLine Multiform Applications November 10, 2018
Closing the File after Writing If the file was opened successfully, close it even if an I/O exception was thrown during the output operations Multiform Applications November 10, 2018
Putting it all together for output Multiform Applications November 10, 2018
Reading an Entire Text File Verify that the file with this name exists Attempt to open the file Read the entire file into a single string variable The above code should be in a try block since many exceptions are possible