Search   Articles   Dev Forums   Personalize   Favorites   Member Login   ASP.Net Hosting
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Sunday, November 23, 2008

Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Classes to Work with Files and Directories


In the design section of the chapter we mentioned that the .NET Framework provides quite a lot of classes to easily manipulate and retrieve information about the file system's items. The System.IO namespace contains all the classes that have to do with the IO operations for any backing store, and some classes that allow us to do advanced stuff such as monitoring the file system and listening for changes (this was pretty hard to do with the Windows API). Since we'll use some of these classes throughout the chapter, it's worth giving a brief description of the most used IO classes:

 

Class

Description

Directory

Provides static (shared) methods for enumerating directories and logical drives, creating/deleting/moving directories and files, and retrieving/editing things like the creation date or the last access date.

DirectoryInfo

Used to work with a specified directory and its subdirectories.

File

Provides static methods for working with files: this includes opening or checking the existence of a file, and appending text data to a file.

FileInfo

Used to work with a specific file.

Path

Performs operations such as extracting the root or the file name from the specified path or combining two path strings.

FileSystemWatcher

Monitors the file system and raises events to handle changes.

Stream

Base class used to read from and write a backing store, such as the file system or network.

StreamReader

Used in conjunction with a stream to read characters from a backing store.

StreamWriter

Used in conjunction with a stream to write characters to a backing store.

TextReader

Abstract class used to define methods for reading characters from any source (backing store, string, and so on).

TextWriter

Abstract class used to define methods for writing characters to any source (backing store, string, and so on).

BinaryReader

Used to read primitive types such as string, integer, and boolean from a stream.

BinaryWriter

Used to write primitive types such as string, integer, and boolean to a stream.

FileStream

Used to read and write data in the file system.

MemoryStream

Used to read and write data in a memory buffer.

 


For a more complete listing of the System.IO namespace's classes and their methods, you can refer to Professional ASP.NET (Wrox Press, ISBN 1-861004-88-5) or Professional C# (Wrox Press, ISBN 1-861004-99-0). We will look at some of the classes described above in action.

Header and Footer Controls

We start our coding with the module-specific controls – in other words the header and the footer. Create a new user control named Header.ascx, and write the following code in the HTML tab of the IDE:

 

<%@ Control Language="c#" AutoEventWireup="false"

    Codebehind="Header.ascx.cs"

    Inherits="Wrox.WebModules.FileManager.Web.Controls.User.Header"%>

<a name="top">

  <table class="MenuTable" border="0" width="100%">

    <tr>

      <td>

        <b><u>FileManager - Wrox WebModule</u></b>

      </td>

      <td align="right">

        <a href="#bottom">

          <img Alt="Go to the bottom of the page"

              src="./Images/GoDown.gif" border="0" />

        </a>

      </td>

    </tr>

  </table>

 

This is simply HTML code (no need to use ASP.NET controls if we don't need to dynamically program them) that creates a title bar and a hyperlink image. This image links to an anchor placed at the bottom of the page, providing a quick way to scroll the page. Note that we associate the MenuTable style to the HTML table. In Chapter 3 we created a stylesheet, ThePhile.css, and began to define styles for use throughout the site. We have now added several new style definitions that we need for our FileManager module. We won't go into the code here – the modified file is available in code download, and you should refer back to Chapter 3 for an explanation of how cascading styles work. This stylesheet is imported by the page that will use this custom control.

 

The only modification we need to make in the code-behind is to change the namespace to Wrox.WebModules.FileManager.Web.Controls.User, which follows the conventions we discussed in Chapter 2.

 

The footer control, similarly, defines a link to jump to the top of the page. It contains an anchor that links from the icon to the header control. Here's the code for Footer.ascx:

 

<%@ Control Language="c#" AutoEventWireup="false"

    Codebehind="Footer.ascx.cs"

    Inherits="Wrox.WebModules.FileManager.Web.Controls.User.Footer"%>

<div align="right" Width="100%">

  <a href="#top">

    <img src="./Images/GoUp.gif"

        Alt="Go to the top of the page" border="0" />

  </a>

</div>

<a name="bottom">


Also, modify the namespace in the code-behind for Footer.ascx in exactly the same way as we did for the header control. In this particular module the header and footer controls are not really necessary, since we have only two pages, but it's a good practice to build them now, as we might want to add new features (and thus other pages) in the future.


DevASP.Net - Disclaimer - Privacy
Copyright © 2008 DevASP.net