Working With Files in C#

A brief and simple explanation of opening, closing, reading, and writing to text files in C#. A few very simple quiz questions at the end.

C Sharpfilesstreamreaderstreamwriter
1.0x

Working With Files in C#

Created 2 years ago

Duration 0:08:24
lesson view count 145
A brief and simple explanation of opening, closing, reading, and writing to text files in C#. A few very simple quiz questions at the end.
Select the file type you wish to download
Slide Content
  1. Working With Files

    Slide 1 - Working With Files

    • Using StreamReader and StreamWriter in C#
  2. Necessary Library

    Slide 2 - Necessary Library

    • Add the System.IO library to the top of the program
    • using System.IO;
    • System.IO; added to existing using statements
  3. File Dialog Boxes

    Slide 3 - File Dialog Boxes

    • OpenFileDialog inFile = new OpenFileDialog();
    • inFile.ShowDialog();
    • String fileName = inFile.FileName;
    • Declare a new Dialog box named inFile
    • Ask the box to show itself
    • Get the name/location of the file the user gave
  4. Opening a File

    Slide 4 - Opening a File

    • StreamReader sr = new StreamReader(inFile.FileName);
    • StreamWriter sw = new StreamWriter(sfd.FileName);
    • Declare a new Stream object to open
    • Provide the name and location of the file to open
  5. Reading a File

    Slide 5 - Reading a File

    • StreamReader sr = new StreamReader(inFile.FileName);
    • while (sr.Peek() >= 0)
    • {
    • line = sr.ReadLine();
    • . . .
    • }
    • sr.Close();
    • Create a new stream object to open
    • Check to see if there is more data to read
    • Close the file when finished
    • Read a line of data
  6. Reading From A File – In A Block

    Slide 6 - Reading From A File – In A Block

    • using (StreamReader sr = new StreamReader(path))
    • {
    • while (sr.Peek() >= 0)
    • {
    • listBox1.Items.Add(sr.ReadLine());
    • }
    • }
    • File opened at beginning of block
    • File closed automatically when block ends
  7. Writing To A File

    Slide 7 - Writing To A File

    • StreamWriter sw = new StreamWriter(sfd.FileName);
    • for (int i = 0; i < 25000; i++)
    • {
    • sw.WriteLine(outputData[i]);
    • }
    • sw.Flush();
    • sw.Close();
    • Open a stream object for writting
    • Write a line of data to the file
    • Flush forces all writes to complete
    • Close the file when finished
  8. Writing To A File – In A Block

    Slide 8 - Writing To A File – In A Block

    • string path = @"c:\Temp\MyTest.txt";
    • using (StreamWriter sw = new StreamWriter(path))
    • {
    • sw.WriteLine("This");
    • sw.WriteLine("is some text");
    • sw.WriteLine("to test");
    • sw.WriteLine("Reading");
    • }
    • Open the file with the using command
    • Write to file inside the block
    • File closed at end of block
  9. Review

    Slide 9 - Review

    • Using System.IO; to make sure IO class libraries are available
    • StreamWriter class to write sequential text files
    • StreamReader class to read sequential text files
    • Specify file name/location in the object declaration
    • .Write to write data and leave mark on the same line
    • .WriteLine to write data and have next data on a new line
    • .ReadLine to read a whole line of data
    • .Read to read a single character at a time
    • .Peek to see if more data remains to be read
    • .Close to release the file
  10. System.IO

    Slide 10 - System.IO

  11. Peek

    Slide 11 - Peek

  12. Writing to a file

    Slide 12 - Writing to a file