ASP.NET
  Home arrow ASP.NET arrow Page 3 - Slapping a Photo Gallery Together in ASP.N...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Slapping a Photo Gallery Together in ASP.NET Part I
By: Harish Kamath
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 30
    2005-04-04

    Table of Contents:
  • Slapping a Photo Gallery Together in ASP.NET Part I
  • Say Cheese
  • Digging Deeper
  • Different Strokes
  • Etcetera
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    More ASP.NET Articles
    More By Harish Kamath


     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT