Files have always been an important aspect of programming. We use files to store data, and in many cases we have to manipulate files and folders from within applications. I do not need to give examples: Just about any application that allows user … [Read more...] about The IONamespace and the FileSystem Component
Accessing Folders and Files
Using the My.Computer.FileSystem Component
Using the My object, you can write some text to a file via a single statement. The WriteAllText method accepts as arguments a path and the string to be written to the file (as well as a third optional argument that determines whether the text will be … [Read more...] about Using the My.Computer.FileSystem Component
Manipulating Folders & Files with the IO Namespace
In this section, you'll learn how to access and manipulate files and folders with the help of the Directory and File classes of the System.IO namespace. The Directory class provides methods for manipulating folders, and the File class provides … [Read more...] about Manipulating Folders & Files with the IO Namespace
The Directory Class of System.IO namespace
The System.IO.Directory class exposes all the members you need to manipulate folders. Because the Directory class belongs to the System.IO namespace, you must import the IO namespace into any project that might require the Directory object's members … [Read more...] about The Directory Class of System.IO namespace
The File Class of System.IO namespace
The System.IO.File class exposes methods for manipulating files (copying them, moving them around, opening them, and closing them), similar to the methods of the Directory class. The names of the methods are self-descriptive, and most of them accept … [Read more...] about The File Class of System.IO namespace
DriveInfo, DirectoryInfo, and FileInfo classes
The IO namespace provides three objects that represent drives, folders, and files: the DriveInfo, DirectoryInfo, and FileInfo classes. These classes, in turn, expose a number of basic properties of the entities they represent. Notice that they're … [Read more...] about DriveInfo, DirectoryInfo, and FileInfo classes
The Path Class
The Path class contains an interesting collection of methods, which you can think of as utilities. The Path class's methods perform simple tasks such as retrieving a file's name and extension, returning the full path description of a relative path, … [Read more...] about The Path Class
The CustomExplorer Application
The CustomExplorer application, which demonstrates the basic properties and methods of the Directory and File classes, duplicates the functionality of Windows Explorer. Its user interface, shown in Figure 11.1, was discussed in section, "The TreeView … [Read more...] about The CustomExplorer Application
Accessing Files
In the first half of the chapter, you learned how to manipulate files and folders. Now we'll discuss how to access files (write data to files and read it back). You've already seen the various Open methods of the File class, which return a Stream … [Read more...] about Accessing Files
The FileStream Class
The Stream class is an abstract one, and you can't use it directly in your code. To prepare your application to write to a file, you must set up a FileStream object, which is the channel between your application and the file. The methods for writing … [Read more...] about The FileStream Class
The StreamWriter Class
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 … [Read more...] about The StreamWriter Class
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 … [Read more...] about The StreamReader Class
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 … [Read more...] about Sending Data to a File
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 … [Read more...] about The BinaryWriter Class
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 … [Read more...] about The BinaryReader Class
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 … [Read more...] about The RecordSave Example
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 … [Read more...] about FileSystemWatcher Component
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 … [Read more...] about The FileSystemWatcher example