Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do i retrieve the directory of multiple images file without hard code ?

How do i retrieve the directory of multiple images file without hard code ?

Scheduled Pinned Locked Moved C#
questiondatabasedata-structurestutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lim Yuxuan
    wrote on last edited by
    #1

    Hi. I had a photo album project and I was wondering is there anyway to retrieve the directory of images with minimum hardcoding ? What I am currently doing now is this : //prefix of the directory of large images const string strLargePrefix = "/images/large/"; //suffix of the directory of large images const string strLargeSuffix = "_large.jpg"; //group the pictures together with each of them assigned with a reference number enum enuImages { //note that the first image will have its reference number //as its position number-1.Example: 2nd image = 2-1 = reference number 1 cHTP3 =0, //1st image cppHTP4 = 1, //second image csharpFEP1 = 2,//third image jHTP4 = 3, //4th image jwsFEP1 =4, //5th image vbnetFEP1 = 5 //6th image } RetrieveData() { // An enumeration object that represents the images based on their // reference number. Initial reference set to 1st image enuImages m_Images = 0; //Array index of the image to be stored at base on their reference number. //Index number has the same value as reference number. int intIndex = 0; //Retrieve directory path of the Large images and sort them for (intIndex = 0; intIndex < int_TotalImages; intIndex++ ) { strLargeImage[intIndex] = Directory.GetCurrentDirectory() + strLargePrefix + m_Images + strLargeSuffix; m_Images++;//increment the reference number to represent next image }// end for loop } I dont really like the idea of grouping the images together because I have to hard code the names of the image file. But I have no choice as it works with the for loop. Is there any method that behaves like the file.ReadAllLines() that can retrieve all the images from that folder and return them as a form of array ?

    B 1 Reply Last reply
    0
    • L Lim Yuxuan

      Hi. I had a photo album project and I was wondering is there anyway to retrieve the directory of images with minimum hardcoding ? What I am currently doing now is this : //prefix of the directory of large images const string strLargePrefix = "/images/large/"; //suffix of the directory of large images const string strLargeSuffix = "_large.jpg"; //group the pictures together with each of them assigned with a reference number enum enuImages { //note that the first image will have its reference number //as its position number-1.Example: 2nd image = 2-1 = reference number 1 cHTP3 =0, //1st image cppHTP4 = 1, //second image csharpFEP1 = 2,//third image jHTP4 = 3, //4th image jwsFEP1 =4, //5th image vbnetFEP1 = 5 //6th image } RetrieveData() { // An enumeration object that represents the images based on their // reference number. Initial reference set to 1st image enuImages m_Images = 0; //Array index of the image to be stored at base on their reference number. //Index number has the same value as reference number. int intIndex = 0; //Retrieve directory path of the Large images and sort them for (intIndex = 0; intIndex < int_TotalImages; intIndex++ ) { strLargeImage[intIndex] = Directory.GetCurrentDirectory() + strLargePrefix + m_Images + strLargeSuffix; m_Images++;//increment the reference number to represent next image }// end for loop } I dont really like the idea of grouping the images together because I have to hard code the names of the image file. But I have no choice as it works with the for loop. Is there any method that behaves like the file.ReadAllLines() that can retrieve all the images from that folder and return them as a form of array ?

      B Offline
      B Offline
      Ben Fair
      wrote on last edited by
      #2

      Sure, it is basically just a file search operation for image file types. Use the DirectoryInfo.GetFiles() method with one of the overloaded versions that allows you to pass a file search pattern (*.gif, for example). The GetFiles() method is an instance method so you have to create an instance of the DirectoryInfo object for the directory you're working with. Also, one of the overloads allows you have it search subdirectories, which is pretty handy if your files may be in multiple subfolders.

      Keep It Simple Stupid! (KISS)

      L 1 Reply Last reply
      0
      • B Ben Fair

        Sure, it is basically just a file search operation for image file types. Use the DirectoryInfo.GetFiles() method with one of the overloaded versions that allows you to pass a file search pattern (*.gif, for example). The GetFiles() method is an instance method so you have to create an instance of the DirectoryInfo object for the directory you're working with. Also, one of the overloads allows you have it search subdirectories, which is pretty handy if your files may be in multiple subfolders.

        Keep It Simple Stupid! (KISS)

        L Offline
        L Offline
        Lim Yuxuan
        wrote on last edited by
        #3

        Thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups