How to get elements path from a folder
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.
Kelvin LokePosted May 3, 2007, 6:01 AM
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!.
Kelvin LokePosted May 2, 2007, 4:30 AM
Mike GoldPosted Apr 30, 2007, 1:28 PM
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:
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));}
}