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? 





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
» Report offensive content