Download Sample .NET Project for FileOperations
This chapter will teach you the basic file operations. In the attached sample application, you can type some text and save into a text file. Also, you can select a text file and display the content in a textbox.
In addition to the basic file operations, you can learn some good programming practices and error handling/validation techniques.
This chapter and the attached sample application demonstrates the usage of following .NET classes and methods:
System.IO.Path.GetExtension() - Method to check the extension of a file.
System.IO.File.Exists() - Check if a file alread exists.
System.Windows.Forms.OpenFileDialog - "File Open Dialog" to choose a file from the disk.
DialogResult.OK - Result from a File Open Dialog.
MessageBox.Show() - Displaying a message to the user.
string.Trim() - Remove white space characters from beginning and end of string.
throw new Exception() - Create and throw a new exception.
System.IO.StreamWriter - Stream writer to write into files.
System.IO.StreamReader - Read from file.
Application.Exit() - Closing an application.