File Access

The ASP.NET File class provides access to the files comprising an application. Through various methods supplied by this class scripts can create and delete files, move and copy files, read and write files, and view file attributes. Again, caution is advised when using File methods since files can be easily deleted without recourse to recovering them.

As in the case with Directory methods, file access is provided through the System.IO namespace which must be imported for the page.

Deleting Files

The File class provides the Delete() method to remove a file from a directory. Its general syntax is shown in Figure 10-14.

File.Delete(path)
Figure 10-14. General format for deleting a file.

The path is the physical path to a folder or it can be a relative path converted with Server.MapPath(). This path can be a literal string or a reference to a variable containing a path string.

File.Delete("c:\eCommerce\BookPictures\Picture.jpg")
File.Delete(Server.MapPath("../BookPictures/Picture.jpg"))
Listing 10-18. Code to delete a file.

Detecting Files

Before working with a file, a test should be made as to whether it exists. When attempting to delete a file, for instance, a run-time error is generated if the file does not exist. The File.Exists() method is used to make this determination.

File.Exists(path)
Figure 10-15. General format for detecting a file.

This method returns True or False depending on whether the identified file path exists. The code to delete a file, then, takes on the following general format.

If File.Exists("path") Then
  File.Delete("path")
End If
Listing 10-19. Code to check for and delete an existing file.

Moving Files

Files can be moved from one location to another by using the File.Move() method whose general format is shown below.

File.Move(source path, destination path)
Figure 10-16. General format for moving a file.

The source path is the path to a current file; the destination path is the path to the new file. If the specified source path does not already exist, or if the destination path already exists, then an error results.

With the following statements the file Picture.gif is moved up a level from the c:\eCommerce\BookPictures folder to the c:\eCommerce folder. During the move the file is deleted from its original location.

If File.Exists("c:\eCommerce\BookPictures\Picture.jpg") _
AND Not File.Exists("c:\eCommerce\Picture.jpg") Then

  File.Move("c:\eCommerce\BookPictures\Picture.jpg", _
            "c:\eCommerce\Picture.jpg")

End If
Listing 10-20. Code to move a file.

Note that the destination path needs to include the name of the file being moved. If you use a different file name in the destination path, then the file is renamed when it is moved.

Copying Files

In a like manner, files can be copied from one folder to another using the File.Copy() method.

File.Copy(source path, destination path)
Figure 10-17. General format for copying a file.

The source path is the physical or relative path to the file being copied, and the destination path specifies a different path with the same or different file name. Unlike using the Move() method, the copied file is not deleted from its original location.

In the following example, a picture file is copied to its parent directory and is assigned a different file name in the destination directory.

If File.Exists("c:\eCommerce\BookPictures\Picture.jpg") _
AND Not File.Exists("c:\eCommerce\Picture.jpg") Then

  File.Copy("c:\eCommerce\BookPictures\Picture.jpg", _
            "c:\eCommerce\Picture.jpg")

End If
Listing 10-21. Code to copy a file.

Renaming Files

The ASP.NET File class does not provide a method for renaming files except when moving or copying them. However, you can use the Visual Basic Rename() function to do this.

Rename(source path, destination path)
Figure 10-18. General format for Visual Basic file Rename function.

The old and new file names must be part of the complete source and destination paths.

Rename("c:\eCommerce\BookPictures\Picture.jpg", _
       "c:\eCommerce\BookPictures\MyPicture.jpg")

Listing 10-22. Code to rename a file.

File Attributes

Three informative file attributes can be retrieved with the File methods shown in Figure 10-19. The GetCreationTime() method returns the date and time at which a file was originally created; the GetLastAccessTime() method returns the date and time when the file was last accessed. The GetLastWriteTime() method returns the date and time at which the file was last written to the directory.

File.GetCreationTime(path)
File.GetLastAccessTime(path)
File.GetLastWriteTime(path)
Figure 10-19. General formats for retrieving file attributes.

Below, for example, this information is displayed for the BooksDB.mdb database.

Show BooksDB.mdb Attributes:

Figure 10-20. Selected attributes of BooksDB.mdb database.
<%@ Import Namespace="System.IO" %>

<SCRIPT Runat="Server">

Sub Show_Attributes (Src As Object, Args As EventArgs)

  Attributes.Text &= "<b>Creation Date: </b>" & _
    File.GetCreationTime("c:\eCommerce\Databases\BooksDB.mdb") & "<br/>"
  Attributes.Text &= "<b>Last Access Date: </b>" & _
    File.GetLastAccessTime("c:\eCommerce\Databases\BooksDB.mdb") & "<br/>"
  Attributes.Text &= "<b>Last Write Date: </b>" & _
    File.GetLastWriteTime("c:\eCommerce\Databases\BooksDB.mdb") & "<br/>"
  
End Sub

</SCRIPT>

<form Runat="Server">

<b>Show BooksDB.mdb Attributes: </b>
<asp:Button text="Get Attributes" OnClick="Show_Attributes" Runat="Server"/>
<p><asp:Label id="Attributes" EnableViewState="False" Runat="Server"/></p>

</form>
Listing 10-23. Code to report file attributes.

File size is not one of the attributes supplied by the File class. This property needs to be accessed through the FileInfo class, requiring you to create a FileInfo object prior to referencing its Length property on the specified file.

Show File Sizes:

Figure 10-21. File sizes in Databases directory.
<%@ Import Namespace="System.IO" %>

<SCRIPT Runat="Server">

Sub Show_File_Sizes (Src As Object, Args As EventArgs)
  
  Dim FileArray() As String
  FileArray = Directory.GetFiles("c:\eCommerce\Databases")
  
  Dim File As String
  For Each File in FileArray
    
    Dim FileName As String
    FileName = Replace(File, "c:\eCommerce\Databases\", "")
    Dim MyFileInfo As New FileInfo("c:\eCommerce\Databases\" & FileName)
    
    Size.Text &= FileName & " = " & MyFileInfo.Length & " bytes<br/>"
    
  Next
  
End Sub

</SCRIPT>

<form Runat="Server">

<b>Show File Sizes: </b>
<asp:Button Text="Get Sizes" OnClick="Show_File_Sizes" Runat="Server"/>
<p><asp:Label id="Size" EnableViewState="False" Runat="Server"/></p>

</form>
Listing 10-24. Code to report file sizes.