Working with Files

Contrary to your probable expectations, you never need to create a new File object—all the File class methods are static, so they're accessible simply by creating a File variable. You can think of a file as a container for data, and you can think of the File object as a direct link to the container itself, a single file on a storage device, but only to the container, not to the contained data. For example, using the File object, you can get and set file dates and times, create files, open files, delete files, copy files, and read and set file attributes, but you can't read the contents using only the File object. You need a Stream object to access a file's contents.

In classic VB, it was easier to attempt to open a file and then trap the resulting error if the file didn't exist than it was to check for the file's existence. In .NET, it's easier to check first and avoid the error. I created a text file named FileStaticMethods.txt in the CSharpASP/ch10 folder. The file contains a text-only copy of the MSDN System.IO.File class static methods that you can use to experiment. Table 10.1 shows the contents of the file in table form.

Table 10.1: System.IO.File Class Static Methods




Creates a StreamWriter that appends text to a file on the specified path or creates the file if it does not already exist.


Overloaded. Copies an existing file to a new file.


Overloaded. Creates a file in the specified fully qualified path.


Creates a StreamWriter that writes a new text file on the specified fully qualified path.


Deletes the file specified by the fully qualified path. An exception is not thrown if the specified file does not exist.


Determines whether a file exists on the fully qualified path.


Gets the FileAttributes of the file on the fully qualified path.


Gets the date and time the specified file was created.


Gets the date and time the specified file was last accessed.


Gets the date and time the specified file was last written to.


Moves a specified file to a new location, providing the option to specify a new filename.


Overloaded. Opens a FileStream on the specified path.


Creates a read-only file on the specified path.


Creates a StreamReader that reads from an existing text file having the specified path.


Creates a read/write Stream on the specified path.


Sets the specified FileAttributes of the file on the specified path.


Sets the date and time the file was created.


Sets the date and time the specified file was last accessed.


Sets the date and time the specified file was last written to.

To illustrate how the File object allows you to read and change information about a file, the Web Form ch10-1.aspx lets you view and change the dates of text files on your server (see Figure 10.1).

The Web Form contains several germane features. To use the Web Form, set the file ch10-1.aspx to the start page and then run the CSharpASP application. Enter a filename into the File Name field. Select Physical Path or Virtual Path and then click the File Info button. If the file exists, you'll see the file's attributes and create, access, or modify date in the scrolling text field. Clicking the button calls a subroutine named showFileinfo (see Listing 10.1).

Figure 10.1: Example Web Form ch10-1.aspx after setting the last access date

Listing 10.1: The showFileinfo Subroutine Retrieves and Displays Information about a File

private void showFileInfo() {

Boolean m virtual;

String m filename;

StringBuilder sb = new StringBuilder(1000);

m filename = txtFilename.Text;

m virtual = PathType.Items[1].Selected;

m filename = Server.MapPath(m filename);

catch (Exception ex) {

lblMsg.Text = ex.Message; txtFilelnfo.Text = ""; return;

if (File.Exists(m_filename)){

sb.Append("Attributes: " +

File.GetAttributes(m filename).ToString() + System.Environment.NewLine); sb.Append("Created: " +

File.GetCreationTime(m filename).ToString() + System.Environment.NewLine); sb.Append("Accessed: " +

File.GetLastAccessTime(m filename).ToString() + System.Environment.NewLine); sb.Append("Modified: " +

File.GetLastWriteTime(m filename).ToString() + System.Environment.NewLine); txtFilelnfo.Text = sb.ToString(); lblMsg.Text = "";

lblMsg.Text = "The file does not exist."; txtFilelnfo.Text = "";

Assume that a user entered the filename /CSharpASP/ch10/FileStaticMethods.txt and then selected the Virtual Path radio button. The code in Listing 10.1 assigns the filename to a private String variable called m_filename, checks the path setting, and sets the Boolean variable m_virtual to true when the user selects the Virtual Path option or false when the user selects the Physical Path option. The File object requires a physical path; therefore, the code uses the Server.MapPath method to translate the virtual path to a physical path when necessary.

Was this article helpful?

0 0

Post a comment