views:

45

answers:

1

Hello,

I have gotten a program Im working on to load some pictures and display them within a listview after using a openfiledialog. What I am looking to do now is take this one step further and auto-load the images from a directory 'icons' within the application directory. Im not too sure how to go about it, So Im going to paste my current code here, and work it from there...

private void loadImageLibraryToolStripMenuItem_Click(object sender, EventArgs e)
    {
        openFileDialog1.Multiselect = true;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if (openFileDialog1.FileNames != null)
            {
                for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
                {
                    addImage(openFileDialog1.FileNames[i]);
                }
            }
            else
                addImage(openFileDialog1.FileName);
        }
    }

    private void addImage(string imageToLoad)
    {
        if (imageToLoad != "")
        {
            imageList1.Images.Add(Image.FromFile(imageToLoad));
            listView1.BeginUpdate();
            listView1.Items.Add(imageToLoad, baseValue++);
            listView1.EndUpdate();
        }
     }

Edit to Clarify: The code provided shows how to load and show the images in a listview control. What Im looking to do now is upon starting the app, load the images automatically from a folder in the programs directory, and then display them in the listview.

A: 

Off the top of my head with no IDE so there may be mistakes! try this

var files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\icons")

files will be an array of strings containing all the files in the directory which you can then loop as you have above using the array

openFileDialog1.FileNames

The \ may not be required before icons, I can't remember if GetDirectoryName drops the trailing \ from the path or not.

you can also pass a filter to GetFiles to only return certain file types.

HTH

EDIT: I have edited the code above to use

System.Windows.Forms.Application.ExecutablePath

rather than

System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase

On testing the code now I have access to an IDE it seems the CodeBase property prepends the path with file:/// which caused my IDE to blow up with an error saying

URI formats are not supported

the code now works in my IDE, You need to make sure your icons Directory is in the same directory as your executable so in my case ....bin\debug\

Give this a try and if it still fails let me know!

OneSHOT
Well, I tried to use the files variable. What I did was in the form_load event, i use a foreach loop for each item in files, and addImage with argument 'item', but it runs out of memory as soon as I start the program. Im not sure where Im going wrong here, since right now, for testing, I have 1 image in the folder.
Rekar
see my edit above
OneSHOT
Wow, That works great. If I wouldve realized that that piece of code was also there, We might not have been through all of this trouble.
Rekar