Slapping Together a Photo Gallery in ASP.NET Part II - Of Albums and Photos
(Page 3 of 7 )
This second example demonstrates some of the more useful static methods of the File() object. Take a look at the code listing below:
<%@ 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 strPhotoFilePath in aryPhotos) {
output.Text += "File Name: " + strPhotoFilePath.Substring(strFileSystemPath.Length + 1) + "<BR>";
output.Text += "Created On: " + File.GetCreationTime(strPhotoFilePath) + "<BR>";
output.Text += "Last Modified On: " + File.GetLastWriteTime(strPhotoFilePath) + "<BR>";
output.Text += "Attributes: " + File.GetAttributes(strPhotoFilePath).ToString();
output.Text += "<HR WIDTH=\"25%\" ALIGN=\"LEFT\">";
}
}
void Page_Load(Object sender, EventArgs e) {
// path to a particular Photo Album
// i.e. folder on the file system
string strPhotoAlbumPath = " E:\\inetpub\\wwwroot\\Gallery\\London";
// check if the specified Photo Album i.e. folder exists
if(Directory.Exists(strPhotoAlbumPath)) {
// get the list of photos
// in a particular Photo Album
GetPhotos(strPhotoAlbumPath);
} else {
output.Text = "Sorry, the folder <U>" + strPhotoAlbumPath + "</U> could not be located.";
}
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>My Gallery</TITLE>
<BASEFONT face="Arial" />
</HEAD>
<BODY>
<asp:Label id="output" runat="server" />
</BODY>
</HTML>
Here is a quick look at the output generated by this script before I get into the mandatory explanations:

Here, I've defined a string variable titled "strPhotoAlbumPath." This stores the path to a selected "Photo Album," i.e. file system folder. Next, I have updated my custom GetPhotos() function - it made a brief appearance in the first article - to display information about each image file that the script encounters in the folder.
Let me take you through the changes: I have defined a string array titled "aryPhotos" to store the locations of the different files. Next, I have invoked the GetFiles() method to initiate the array. A simple "foreach" loop allows me to iterate through the array and to display relevant file information using the following methods:
- GetCreationTime() returns the date and time when the file, in question, was created.
- GetLastWriteTime(), as the names suggests, it returns the time when the file was last modified.
- GetAttributes() returns the attributes in the form of a FileAttribute() enumeration value.
And there's a lot more. Review the following URL for more information: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIOFileClassTopic.asp
A final note, before I move to the next section: a simple "if-else" loop in the Page_Load() function ensures that the server does not spit a screen full of errors if the specified folder does not exist. This is evident from the output shown below:

Next: Two Sides of a Coin >>
More ASP.NET Articles
More By Harish Kamath