Slapping a Photo Gallery Together in ASP.NET Part I - Say Cheese
(Page 2 of 6 )
Before I get into the nitty gritty of coding the "Photo Gallery" application, let me give you a little note on how I've organized my photo collection on the server. I've already hinted that I plan to use the file and folder management classes available on the .NET platform in order to develop this application. So, it logically follows that the most convenient manner to organize the photographs on the file-system is by creating a folder structure that reflects the location and purpose of each vacation.
Consider the following folder structure:
E:\inetpub\wwwroot\
+---Gallery
|
+---Australia
| +---Christmas
| \---New_Year
+---London
| \---The_Tube
+---New York
| \---Independence_Day
\---Singapore
\---Shopping_Festival
As seen above, the folder structure is almost self-explanatory as far as the location and purpose of each vacation is concerned. You can easily follow a similar naming convention for each image file in each album and voila - you do not need to worry about storing the descriptions for your photographs separately!
Next, you have to look into the permissions assigned to the entire folder structure that forms a part of the application. It is essential to ensure that the special "ASPNET" user - created at the time of installation of the .NET framework - has the required permissions (especially if you wish to create folders dynamically using ASP.NET scripts) across the entire folder structure, listed above.
Now, its time to get our hands dirty with some code - the following example introduces the Directory() object:
<%@ Page Language="C#" %>
<%@ import Namespace="System.IO" %>
<SCRIPT runat="server">
void Page_Load(Object sender, EventArgs e) {
// path to the root folder of the "Photo Gallery"
string strPhotoGalleryRoot = "E:\\inetpub\\wwwroot\\Gallery";
// check if the above folder exists
if(Directory.Exists(strPhotoGalleryRoot)) {
output.Text = "The folder <U>" + strPhotoGalleryRoot + "</U> found.";
} else {
output.Text = "Sorry, the folder <U>" + strPhotoGalleryRoot + "</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>
Save this script under the root folder of the Web server and load it into your favorite browser to view the one of the following outputs:
1. If the "Gallery" folder, stored in the "strPhotoGalleryRoot" variable, exists.

2. If the "Gallery" folder does not exist at the specified location.

Coming back to the code listing: for starters, I've imported the "System.IO" namespace as the Directory() object as part of the "System.IO" .NET assembly. As you may already know, it is recommended that you import the required namespaces using the "import" directive, as it allows you to create object instances without having to provide the complete class path at the time of instantiation.
Next, I've defined a variable titled "strPhotoGalleryRoot" - as the name suggests, I use this string variable to store the path to the root folder of my "Photo Gallery."
It's time to say hello the Directory() object. This .NET object, equipped with a bunch of static methods, allows you to work with any folder located on the file system of the Web server. For the uninitiated, static methods are object methods that can be invoked without the need to create an object instance of the underlying class.
Finally, the above example demonstrates the use of the Exists() static method of the Directory() object - it returns a "true" value if a particular path exists or a "false" value otherwise.
Next: Digging Deeper >>
More ASP.NET Articles
More By Harish Kamath