alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow VB.NET 2005 arrow Working with File System in .NET
Site Search


Working with File System in .NET
Article Index
Working with File System in .NET
Page 2

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.

Access and Manipulate Data - Working with Disk Files

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.

  1. Create a new windows application in the Visual Basic Express and
  2. Give the name of the project as FileBrowse1.
  3. To the form Form1 add two Labels, a TextBox , a CheckBox, and two Buttons.
  4. Arrange the controls and name them as shown in the screenshot shown below:

Now add the necessary code to the application. The code for the application is shown below:

Click here to veiw sample code

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:

Streams

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. Reading. Transfer of data from the stream into a data structure, such as an array of bytes.

2. Writing to streams – Transferring data from a data structure into a stream.

3. Support to seeking – Querying and modifying of the current position within a stream.

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.

Using FileStream Class

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:

  • Create a text file.
  • Write to a text file.
  • Read from a text file.
  • Append text to a file.
  • Rename or move a file.
  • Delete a file.
  • Copy a file.
  • Get the size of a file.
  • Get the attributes of a file.
  • Set the attributes of a file.
  • Determine if a file exists.
  • Read from a binary file.
  • Write to a binary file.
  • Retrieve a file extension.
  • Retrieve the fully qualified path of a file.
  • Retrieve the file name and extension from a path.
  • Change the extension of a file.


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.

Click here to view sample code

Now press F5 to execute the file. The outputs generated by the program are shown below:

.

.


 
< Prev   Next >
Sponsored Links
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape