File I/O classes introduction

This post will highlight the classes that are typically used for file I/O.

.

While learning about streams, you were introduced to two methods in the static File class. Let’s discuss a few others, and talk about all the useful classes.

There are three static I/O classes that we will work with. (You do not have to create an instance of these static classes, you can just use their members directly.) The introductory text snippets in the sections below are taken from the MSDN articles.

When working with files, directories, and paths, the path+file name must be fully-formed and syntactically correct. Make sure you understand virtual and physical paths (review the ASP.NET Web Site Paths document if you need to). In web applications, we often use the Server.MapPath() method and Request.Url property to provide some of the information we need in our code.

Also, please remember that you must use impersonation to enable your code to do more than just read a file or directory.

.

File Class

Use the File class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to files. You can also use the File class to get and set file attributes or DateTime information related to the creation, access, and writing of a file.

Many of the File methods return other I/O types when you create or open files. You can use these other types to further manipulate a file. For more information, see specific File members such as OpenText, CreateText, or Create.

Because all File methods are static, it might be more efficient to use a File method rather than a corresponding FileInfo instance method if you want to perform only one action. All File methods require the path to the file that you are manipulating.

.

Directory Class

Use the Directory class for typical operations such as copying, moving, renaming, creating, and deleting directories. You can also use the Directory class to get and set DateTime information related to the creation, access, and writing of a directory. [The Directory class also has members that will return lists as string arrays; these lists can include (for example) the directory’s file names, directory names, or both.]

Because all Directory methods are static, it might be more efficient to use a Directory method rather than a corresponding DirectoryInfo instance method if you want to perform only one action. Most Directory methods require the path to the directory that you are manipulating.

.

Path Class

A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform. For example, on some systems, a path can start with a drive or volume letter, while this element is not present in other systems.

The members of the Path class enable you to quickly and easily perform common operations such as determining whether a file name extension is part of a path, and combining two strings into one path name. [Other members enable you to extract portions of the path, such as the path name only, or the filename+extension only.]

A path can contain absolute or relative location information. Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location.

.

Instance classes – FileInfo Class, and DirectoryInfo Class

There are two instance I/O classes that we may work with:

.

FileInfo Class

The static methods of the File class [above] perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of FileInfo instead, because the security check will not always be necessary.

.

DirectoryInfo Class

The static methods of the Directory class [above] perform security checks on all methods. If you are going to reuse an object several times, consider using the corresponding instance method of DirectoryInfo instead, because the security check will not always be necessary.

.

File Class read and write methods specifically for text data

The File class offers methods that enable you to easily (in terms of syntax and usage) read from or write to a text file. Check them out on your own:

  • ReadAllText
  • ReadAllLines
  • WriteAllText

.

Example using the Directory and Path classes

The “…Directory” example shows how the Directory and Path classes can be used. The example reads a list of files in a directory, and then creates a BulletedList of hyperlinks to the individual files. This is an easy way to create a dynamically-generated list of file system items. (So… that’s how the code examples page gets built!)

Make sure you check the BTI420 Virtual Textbook topics for overview, how-to, and reference coverage of these topics.

.


  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: