How do i retrieve the directory of multiple images file without hard code ?
-
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 ?
-
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 ?
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). TheGetFiles()
method is an instance method so you have to create an instance of theDirectoryInfo
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)
-
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). TheGetFiles()
method is an instance method so you have to create an instance of theDirectoryInfo
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)
Thanks