Home

File Processing

 

The .NET Support for Files

 

Introduction

The primary support of a file as an object is provided by a .NET Framework class called File. This Shared class is equipped with various types of (static) methods to create, save, open, copy, move, delete, or check the existence of a file. As an alternative, the Microsoft Visual Basic library provides the My object that includes the FileSystem object in the Computer object.

     

File Existence

One of the valuable operations that the File class can perform is to check the existence of the file you want to use. For example, if you are creating a new file, you may want to make sure it does not exist already because if you try to create a file that exists already, the compiler may first delete the old file before creating the new one. This could lead to unpredictable results, especially because such a file is not sent to the Recycle Bin. On the other hand, if you are trying to open a file, you should first make sure the file exists, otherwise the compiler will not be able to open a file it cannot find.

To check the existence of a file, the File class provides the Exists method. Its syntax is:

Public Shared Function Exists(path As String) As Boolean

To perform this operation using the FileSystem class from My, you can call its FileExists() method whose syntax is:

Public Function FileExists(ByVal file As String) As Boolean

If you provide only the name of the file, the compiler would check it in the folder of the application. If you provide the path to the file, the compiler would check its drive, its folder(s) and the file itself. In both cases, if the file exists, the method returns True. If the compiler cannot find the file, the method returns False. It is important to know that if you provide a complete path to the file, any slight mistake would produce a False result.

File Creation

Besides checking the existence of the file, the File class can be used to create a new file. To support this operation, the File class is equipped with the Create() method that is overloaded with two versions as follows:

Public Shared Function Create(path As String) As FileStream
Public Shared Function Create(path As String, bufferSize As Integer) As FileStream

In both cases, the File.Create() method returns a Stream value, which is a FileStream value. As the File.Create() method indicates, it takes the name or path of the file as argument. If you know or want to specify the size, in bytes, of the file, you can use the second version.

To provide the same operation of creating a file, you can use the Open() method of the File class. It is overloaded in three versions as follows:

Public Shared Function Open ( _
	path As String, _
	mode As FileMode _
) As FileStream

Public Shared Function Open ( _
	path As String, _
	mode As FileMode, _
	access As FileAccess _
) As FileStream
Public Shared Function Open ( _
	path As String, _
	mode As FileMode, _
	access As FileAccess, _
	share As FileShare _
) As FileStream

Access to a File

In order to perform an operation on a file, you must specify to the operating system how to proceed. One of the options you have is to indicate the type of access that will be granted on the file. This access is specified using the FileAccess enumerator. The members of the FileAccess enumerator are:

  • FileAccess.Write: New data can be written to the file
  • FileAccess.Read: Existing data can be read from the file
  • FileAccess.ReadWrite: Existing data can be read from the file and new data be written to the file

File Sharing

In standalone workstations, one person is usually able to access and open a file then perform the necessary operations on it. In networked computers, you may create a file that different people can access at the same time or you may make one file access another file to retrieve information. For example, suppose you create an application for a fast food restaurant that has two or more connected workstations and all workstations save their customers orders to a common file. In this case, you must make sure that any of the computers can access the file to save an order. An employee from one of these workstations must also be able to open the file to retrieve a customer order for any necessary reason. You can also create a situation where one file holds an inventory of the items of a store and another file holds the customers orders. Obviously one file would depend on another. Based on this, when an operation must be performed on a file, you may have to specify how a file can be shared. This is done through the FileShare enumerator.

The values of the FileShare enumerator are:

  • FileShare.Inheritable: Allows other file handles to inherit from this file
  • FileShare.None: The file cannot be shared
  • FileShare.Read: The file can be opened and read from
  • FileShare.Write: The file can be opened and written to
  • FileShare.ReadWrite: The file can be opened to write to it or read from it

The Mode of a File

Besides the access to the file, another option you will most likely specify to the operating system is referred to as the mode of a file. It is specified through the FileMode enumerator. The members of the FileMode Enumerator are:

  • FileMode.Append: If the file already exists, the new data will be added to its end. If the file doesn't exist, it will be created and the new data will be added to it
  • FileMode.Create: If the file already exists, it will be deleted and a new file with the same name will be created. If the file doesn't exist, then it will be created
  • FileMode.CreateNew: If the new already exists, the compiler will throw an error. If the file doesn't exist, it will be created
  • FileMode.Open: If the file exists, it will be opened. If the file doesn't exist, an error would be thrown
  • FileMode.OpenOrCreate: If the file already exists, it will be opened. If the file doesn't exist, it will be created
  • FileMode.Truncate: If the file already exists, its contents will be deleted completely but the file will be kept, allowing you to write new data to it. If the file doesn't exist, an error would be thrown

Starting and Closing a Stream

 

Starting a Stream

File streaming consists of performing one of the routine operations on a file, such as creating or opening it. This basic operation can be performed using a class called FileStream. You can use a FileStream object to get a stream ready for processing. As one of the most complete classes of file processing of the .NET Framework, FileStream is equipped with all necessary properties and methods. To use it, you must first declare a variable of it. The class is equipped with nine constructors.

One of the constructors of the FileStream class has the following syntax:

Public Sub New(path As String, mode As FileMode)

This constructor takes as its first argument the name of the file or its path. The second argument specifies the type of operation to perform on the file. Here is an example of calling this method:

Imports System.IO

Public Class Exercise

    Private Sub btnSave_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnSave.Click
        Dim Filename As String = "Persons.prs"

        Dim StreamPersons As FileStream = New FileStream(Filename, FileMode.Create)
   
    End Sub
End Class

Closing a Stream

When you use a stream, it requests resources from the operating system and uses them while the stream is available. When you are not using the stream anymore, you should free the resources and make them available again to the operating system so that other services can use them. This is done by closing the stream.

To close a stream, you can can call the Close() method of the class(es) you were using. Here are examples:

Public Class Exercise

    Private Sub btnSave_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles btnSave.Click
        Dim Filename As String = "Persons.prs"

        Dim StreamPersons As FileStream = New FileStream(Filename, FileMode.Create)
   
        StreamPersons.Close()
    End Sub
End Class

 

 

Previous Copyright © 2008-2016, FunctionX, Inc. Next