In this tutorial you will learn about Access and Manipulate Data, Working with Disk Files, Browsing for Files, Streams and Backing Stores, Using the FileStream Class, Using the StreamReader and StreamWriter Classes, Using the BinaryReader and BinaryWriter Classes.
Working with Disk Files
Temporary storage of data as in the illustrations above is not the optimal solution. Persistent data storage is a file having a collection of bytes and is stored as data streams. All read write operations to these data steams can be done with ease in the circumstances. In the following illustration we shall see how read write operations can be performed on persistent data storage.
Browsing For Files
The System.IO.Directory class can be used for typical operations such as copying, moving, renaming, creating and deleting directories. This is a static class and therefore is efficient in the performance of single operations. This class can be used to perform operations relating to browsing for files, The System.IO.FileInfo.Name method of this class used to see the files in the directory. The System.IO.Directory.GetDirectories or System.DirectoryInfo.GetDirectories methods enable users see the sub directories in a the directory. Let us understand the issue by the following example.
Now add the necessary code to the application. The code for the application is shown below:
Now press F5 to execute the application. In the window type the directory name that we want to browse and also use the check box to specify if we want to view the file names or not. Now click the button Browse to view the file names and the directory names. The outputs generated for both with and without the check box selected are shown below:
Stream is the abstract base class of all streams. A stream is an abstraction of a sequence of bytes, such as a file, an input/output device, an inter-process communication pipe, or TCP/IP socket. The stream class and its derived classes provide generic view of these different types of input and output, isolating the programmer from the specific details of the operating system and the underlying devices.
Streams are concerned with three fundamental operations:
1.2.3.Depending on the underlying data source or repository, streams might support only some of the several capabilities. An application may query a stream for its capabilities by using the System.IO.Stream.CanRead, System.IO.Stream.CanWrite, and System.IO.Stream.CanSeek properties.
The FileStream class gives the user the capability to read from, write to, open, and close files on a file system. This class also can be used to manipulate other file related handlers like pipes, standard input, and standard output. The user can also specify read and write operations to be either Synchronous or asynchronous.
Some of the functions ofthe FileStream class are given below:
The following example will illustrate some of the file operations. The two programs that are included will perform functions like reading the directory path, creating a file and listing it and also writing to a file and reading from a file.
1. Create a new Windows Application in Visual Basic Express.
2. Add a Label and three buttons and arrange them as shown in the screenshot below:
Add to the form the following code and save the project.