Skip to content

How to get elements path from a folder

Kelvin Loke

Asked Apr 30, 2007, 2:06 AM EDT3 answers
Share
LinkedInX
Hi everyone, I´m developing an Image Analyzer in C#, this program open an Image file an analyze it. What I now need to do is, instead of opening just 1 file ( one image ) I need to select a whole folder that contains images in order to analyze all without having to open them one by one. When I open every element of the folder what I just need to obtain is the path of the image... Can Anyone help me with this? Thank you very much!! Kelvin.

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    there are two ways you can do this:

    1)  You can use the FolderBrowserDialog to select a folder and then loop through the files you want:
    2)  you can set the OpenFileDialog for Multiple File Section Mode.

    Here is solution #1:

    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)

    {

    listBox1.Items.Clear();

    string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpg");

    foreach (string file in files)

    {

    listBox1.Items.Add(Path.GetFileName(file));

    }

    }


    Here is Solution #2:

    C#
    openFileDialog1.Multiselect = true;
    openFileDialog1.FileName = "*.jpg";
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    listBox1.Items.Clear();
    foreach (string file in openFileDialog1.FileNames)
    {
    listBox1.Items.Add(Path.GetFileName(file));
    }
    }
    0
  • 0
    Thank you very much for your time Mike, I´ll try it this afternoon, hope that I can manage to make it work!,, I´ll post back soon after tryin´it, thanks again.
     
    0
  • 0
    Hey again!, I tryed it and worked perfectly!!!, here´s what I did:

    OpenFileDialog ofn = new OpenFileDialog();
    ofn.Multiselect = true;
    ofn.FileName = "*.jpg";
    if (ofn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    String fileName = ofn.FileName;

     foreach (string file in ofn.FileNames)
                  {                  
                      listBox1.Items.Add(Path.GetFullPath(file)); 
                  }

    }


    I changed 

    listBox1.Items.Add(Path.GetFileName(file));

    for

    listBox1.Items.Add(Path.GetFullPath(file)); 

    coz I needed the fullpath instead of the fileName, so as I said before, it works!
    Thank you very much for your help, see ya!.
    0