Slapping a Photo Gallery Together in ASP.NET Part I - Digging Deeper
(Page 3 of 6 )
While I'll agree that the example in the previous section wasn't as complex as a session on "Theory of Relativity" would be, it does its job of laying down a foundation on which I can build further. Take a peek at the next code listing; that should convince you of the utility of this Directory() object.
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.IO" %>
<SCRIPT language="C#" runat="server">
// custom function to return the list of photographs
// a.k.a. image files for a particular file system location
void GetPhotos(string strFileSystemPath) {
// define array to store list of "Photos"
string[] aryPhotos;
// use the GetFiles() static method
// to obtain a list of image i.e. JPEG files
aryPhotos = Directory.GetFiles(strFileSystemPath, "*.jpg");
// iterate over array of "Photos" to
// get details of each Photo
foreach(string strPhoto in aryPhotos) {
output.Text += "<LI> Photograph Name: <U>" + strPhoto.Substring(strFileSystemPath.Length + 1) + "</U>";
}
}
// custom function to return the list of photo albums
// a.k.a folders for a particular file system location
void GetPhotoAlbums(string strFileSystemPath) {
// define array to store list of "Photo Galleries"
string[] aryPhotoAlbums;
// check if the current folder exists
if(Directory.Exists(strFileSystemPath)) {
// use the GetDirectories() static method
// to obtain a list of sub-folders
aryPhotoAlbums = Directory.GetDirectories(strFileSystemPath);
output.Text += "<UL>";
// iterate over array of "Photo Album" to
// get details of each Photo Album
foreach(string strPhotoAlbum in aryPhotoAlbums) {
output.Text += "<LI> Photo Album Title: <U>" + strPhotoAlbum.Substring(strFileSystemPath.Length + 1) + "</U>";
GetPhotoAlbums(strPhotoAlbum);
// invoke the GetPhotos() custom function
// to list the "Image Files" in a particular folder
GetPhotos(strFileSystemPath);
output.Text += "</UL>";
} else {
output.Text += "Sorry, the folder <U>" + strFileSystemPath + "</U> could not be located.";
}
}
void Page_Load(Object sender, EventArgs e) {
// path to the root folder of the "Photo Gallery"
string strPhotoGalleryRoot = "E:\\inetpub\\wwwroot\\Gallery";
// function to retrieve the Albums
GetPhotoAlbums(strPhotoGalleryRoot);
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>My Gallery</TITLE>
<BASEFONT face="Arial" />
</HEAD>
<BODY>
<asp:Label id="output" runat="server" />
</BODY>
</HTML>
This is what the output of the script should like for a two level "Photo Gallery" - the top level consists of albums, i.e. folders, and the second level consists of photographs, i.e. image files.

Evident from the output, the above code listing iterates over my two-level "Photo Gallery" folder structure and displays the names of the sub-folders under the root folder, followed by the names of the files (with a "jpg" extension only) present in each sub-folder.
Time for some explanation. Let me first focus on the de facto Page_Load() function. Here, I've already stated the purpose of the "strPhotoGalleryRoot" string variable. So, let me skip straight to the GetAlbums() custom function.
As the name suggests, the GetAlbums() function takes a file-system path to a folder and uses the GetDirectories() method of the Directory() object to retrieve an array of string values. Each value in this array represents the name of a sub-folder (a.k.a. "Albums" in our custom "Photo Gallery" terminology) of the root folder (passed as an input parameter).
Within my GetPhotoAlbums() custom function, I've defined an array - aryPhotoAlbums[] - to store the list of sub-folders, as described above. Next, I perform a little check to see if the current folder exists. If it does, I've invoked the GetDirectories() method and stored the return array value in the aryPhotoAlbums[] array.
Next, I've used the "foreach" loop to iterate through my aryPhotoAlbums[] array. Once again, I've invoked the GetPhotoAlbums() function to fetch subsequent sub-folders; this recursive mechanism ensures that the above script is capable of handling an n-level folder hierarchy without any changes.
Finally, I have defined another custom function called GetPhotos(), this uses the GetFiles() static method of the Directory() object to retrieve an array containing the names of the files present in a particular folder.
And since I am putting together a "Photo Gallery," it makes sense to restrict the contents of the array to image files. Hence, the second optional parameter of the GetFiles() method; this allows me to specify a pattern, and the list will be filtered accordingly. For example, the pattern *.jpg returns a list of files with the "jpg" extension.
One more point that I would like to highlight here is the use of the Substring() method to format the output by stripping of the file-system path up to the file (or folder) in question.
Before we move to the next section, here is a quick peek at the output if the "root" folder specified in the "strPhotoGalleryRoot" does not exist:

Next: Different Strokes >>
More ASP.NET Articles
More By Harish Kamath