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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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: 3 stars3 stars3 stars3 stars3 stars / 31
    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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek