The .NET Framework comes fully equipped with a set of classes to carry out basic file system tasksââ,¬"creating and reading directories, checking to see if files exist, retrieving file properties, and so on. Most of this magic takes place through two built-in objects, the FileInfo() object and the DirectoryInfo() object, both part of the System.IO namespace.

In this article, I will introduce you to these two objects. I'll show you how to read directories, retrieve file properties, and create a Windows Explorer clone that dynamically generates directory listings and file information.

Checking if a directory exists
My first example, Listing A, is a simple ASP.NET script that checks for the existence of a particular directory. This is a critical step when developing applications that interact with the file system, and one that many newbie programmers forget.

Listing A

As you will see when you try running the script above, the code tells you whether the named directory exists or not. It's able to do this because of the Exists property of the DirectoryInfo() class, which I imported as part of the System.IO namespace at the beginning of the script.

The class constructor requires an input argumentââ,¬"the name of the directory to be tested. The Exists property then stores a Boolean valueââ,¬"true if the named directory exists and false if it doesn't.

Important: The special "ASPNET" user must have the privileges necessary to view the named directory in order for the script to work correctly.

Dude, where's my file?
Now that you've been introduced to the DirectoryInfo() class, let's turn our attention to files and the FileInfo() object. Take a look at the script in Listing B. This expands on the functionality in Listing Aââ,¬"it tests for the presence of a file, and then prints more information about it if it exists.

Listing B

If the named file exists, here is what the output of the script above might look like:

The file E:/users/john/personal/files/Test_Document.doc
was found. Here is more information:
Name: Test_Document.doc
Location: E:/users/john/personal/files/Test_Document.doc
Created on: 08/07/2004 12:54:00
Last modified on: 08/07/2004 12:54:00
File size (in bytes): 10752
Extension: .doc

Obviously, if the file cannot be found, you'll see an error message telling you all about it.

As before, I've begun by creating an instance of the FileInfo() object, and passing the name and location of the file to the constructor. Next, I use the Exists property of the FileInfo() object to check if the file exists. If it does, I use a bunch of other properties of the FileInfo() object to obtain more information about the file:

  • the Name property, which stores the name of the file
  • the FullName property, which stores the full path to the file (including the drive letter)
  • the CreationTime and LastWriteTime properties, which represent the times the file was originally created and last modified
  • the Length property, which stores the size of the file in bytes (note my use of the ToString() method to convert the number into a string)
  • the Extension property, which stores the file extension

There are more properties tooââ,¬"take a look at Microsoft's documentation for more information. And incidentally, you can use these same properties for the DirectoryInfo() class as well. Try it for yourself (after reading up on the class) and see.

Do you need help with .Net? Gain advice from Builder AU forums

Related links

Comments

1

roshan - 09/04/08

hey i m getting an error saying Unknown User or Bad Password...
when i try to get the information of files containing in a directory that is located somewhere in network...
Any solutions please....
Thanks

DirectoryInfo di = new DirectoryInfo("\\Some Network Path");
FileInfo[] fi = di.getFiles();

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

roshan - 04/09/08

hey i m getting an error saying Unknown User or Bad Password... when i try to get the information of files containing ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!