The StreamWriter class is the channel through which you send data to a text file. To create a new StreamWriter object, declare a variable of the StreamWriter type. The first overloaded form of the constructor accepts a file’s path as an argument and creates a new StreamWriter object for the file: The new object has… [Continue Reading]
Accessing Folders and Files
The StreamReader Class
The StreamReader class provides the necessary methods for reading from a text file and exposes methods that match those of the StreamWriter class (the Write and WriteLine methods). The StreamReader class’s constructor is overloaded. You can specify the FileStream object it will use to read data from the file, the encoding scheme, and the buffer… [Continue Reading]
Sending Data to a File
The statements in Listing 11.11 demonstrate how to send various data types to a file. You can place the statements of this listing in a button’s Click event handler and then open the file with Notepad to see its contents. Everything is in text format, including the numeric values. Don’t forget to import the System.IO… [Continue Reading]
The BinaryWriter Class
To prepare your application to write to a binary file, you must set up a BinaryWriter object, with the statement shown here, where FS is a properly initialized FileStream object: You can also create a new BinaryWriter class directly on a file with the following form of the constructor: To specify the encoding of the… [Continue Reading]
The BinaryReader Class
The BinaryReader class provides the methods you need to read data from a binary file. As you have seen, binary files might also hold text, and the BinaryReader class provides the ReadString method to read strings written to the file by the WriteString method. To use the methods of the BinaryReader class in your code,… [Continue Reading]
The RecordSave Example
Let’s look at the code for saving structured information to a binary file. In this section, you’ll build the RecordSave application, which demonstrates how to store a price list to a disk file and read it later from the same file. The main form of the application is shown in Figure 11.2. The Save Records… [Continue Reading]
FileSystemWatcher Component
FileSystemWatcher is a special component that has no visible interface and allows your application to monitor changes in the file system. You can use the FileSystemWatcher component to monitor changes in the local computer’s file system, a network drive, and even a remote computer’s file system (as long as the remote machine is running Windows… [Continue Reading]
The FileSystemWatcher example
The FileSystemWatcher project, shown in Figure 11.3, demonstrates how to set up a FileSystemWatcher component and how to process the events raised by the component. The FileSystemWatcher component is initialized when the Start Monitoring button is clicked. This button’s Click event handler prepares the FileSystemWatcher component to monitor changes in text files on the root… [Continue Reading]