Using the Directory and Directory Info Classes

Just as you use the File and FileInfo classes to manipulate files, you can use the Directory and DirectoryInfo classes to manipulate directories. The relationship between the Directory and the DirectoryInfo classes is similar to the relationship between the File and FileInfo classes. For example, the Web Form ch10-4.aspx uses the Directory class static method Exists to show that the current directory exists (rather obvious, but guaranteed to work). Listing 10.5 shows the code from the ch10-4.aspx.cs code-behind Page_Load method that lists the current directory contents.

Listing 10.5: Using the Directory and DirectoryInfo Classes (ch10-4.apsx.cs)

private void Page Load(object sender, System.EventArgs e) { DirectoryInfo di;

Response.Output.NewLine = "<br>";

Response.Output.WriteLine("<h3>Using Directory</h3>"); Response.Output.Write("The path " +

Server.MapPath(".") + " exists="); Response.Output.WriteLine(Directory.Exists (Server.MapPath(".")).ToString());

foreach (String s in

Directory.GetFiles(Server.MapPath("."))) { Response.Output.WriteLine(s);

Response.Output.WriteLine

("<h3>Using DirectoryInfo</h3>");

di = new DirectoryInfo(Server.MapPath("."));

Response.Output.Write(("The path " + di.FullName + " exists="));

Response.Output.WriteLine(di.Exists.ToString());

foreach (FileInfo fi in di.GetFiles()) { Response.Output.WriteLine(fi.FullName);

The example shows you how to use the basic methods of the Directory and DirectoryInfo classes. In addition, the code in Listing 10.5 shows you how to overcome the rather unfortunately missing Response.WriteLine method by using the Output property to gain access to the Response object's underlying Stream object—which, as you saw in the preceding section, has a NewLine property and a WriteLine method. As with StringWriter, you can combine these to write lines of HTML without manually concatenating the <br> tag to each line. This is convenient when you need to write several lines in a row. Be careful when you use this technique, because it appends the <br> tag to every Response.Write statement until you reset the NewLine property to its default. On my machine, the output from Listing 10.5 looks like Figure 10.2.

Figure 10.2: Directory listing: output from the ch10-4.aspx Web Form

As a nice bonus, the FileInfo object performs tasks for you that you used to have to perform manually. For example, how many times have you written code to strip the file extension from a filename or to find just the filename or path? You don't have to do that anymore. As an example, here's the output from an alternate directory listing (see Figure 10.3).

Mii * Jfl 1 r.T> Mjm. > j-.:r-3,. » 0 4 fr S -®4 ■ f ill* DSmt«hti1 aripali rtiimri di C .Sh^jee 'hsp i.blko

. v.!•afjjb-' wm¿CSbuikTiih 10 >Y,0 311 i t \ ■ : r dIO J niK-Rit fan IUG1(-*vb utt dliHtiuj^Hi :>

II$MK44( em

: «Jt-1 "»iwi• ■:f-CJjii'-ttii1 5 rlilti'Z «ip iL <i ai

■ •«■iiflf1 vwwi <¿Cfwj-AJiP.ih 10 iiiti«■ g 11 tm fim . !•afjjb-'wm¿CSbuikTiih10 ti'O ] n|j w¡?i

Figure 10.3: Directory listing: output from the ch10-4.aspx Web Form

The Web Form ch10-5.aspx contains the code that generated this output:

private void Page Load(object sender, System.EventArgs e) { DirectoryInfo di = new

DirectoryInfo(Server.MapPath(".")); Int32 counter=0;

Response.Write("<h3>Using Directory " +

Server.MapPath(".") + "</h3>"); Response.Write("<table cellpadding='4' " + "cellspacing='5'><tr><th>Path</th><th>" + "File name</th><th>Extension</th></tr>"); foreach (FileInfo fi in di.GetFiles()) { Response.Write("<tr"); if (counter % 2 == 0) {

Response.Write(" style='background: " + "lightyellow'>");

Response.Write(" style='background: lightblue'>");

Response.Write("<td>" + fi.DirectoryName + "</td>"); Response.Write("<td>" + fi.Name + "</td>"); Response.Write("<td>" + fi.Extension + "</td>"); Response.Write("</tr>"); counter += 1;

Although this example is much better looking than the ch10-4.aspx Web Form, it is not a good example of using Web Forms or code-behind classes to generate output. Why not? Because the HTML is bound up in the code; in other words, writing HTML output in this manner defeats one of the primary purposes of Web Forms—to separate code from the user interface. It would be much better to use one of the intrinsic ASP.NET server controls and bind it to the data. You'll see more about that in Chapter 14, "Accessing Data."

Using the Directory or DirectoryInfo class, you can perform tasks such as creating new directories or subdirectories. You'll see an example in the next section.

Was this article helpful?

0 0

Post a comment