-1

The problem that I am trying to figure out is: how to populate a listbox with the names of the images, that I have in a folder within my Visual Studio Project? My code so far:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        for (int i = 1; i < 8; i++)
        {
            ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(""));
        }
    }
}

Basically, I have 7 images in a folder(within my actual visual studio project) and I want the for loop to populate the listbox with the names of the images, but without the extension. I can't seem to figure out how to get the directory to work. If anyone could help me, I would greatly appreciate it. I am having trouble with this.

After this, I want to use an 'if' statement to set the image displayed based on which item the user clicks on in the listbox, if that makes sense. I do not know the code for if the item = a certain item, I can display an image based on what they select.

I probably worded this really bad, but it's the issue I am currently experiencing, any help would be greatly appreciated, thank you.

Ahmed ilyas
  • 5,502
  • 8
  • 37
  • 68
Derek S
  • 7
  • 2
  • Is this a ASP.NET project? Where exactly is the folder located? What is the name of the folder? – keenthinker Mar 15 '15 at 17:25
  • Yes, this is an asp.net project and I a coding this in C# the folder is named 'images', it contains seven pictures and the folder was created within my project, in the root. I need to display the names of each picture file into the listbox without the .jpg extension, using a for loop. – Derek S Mar 15 '15 at 17:27
  • You need to use the [Server.MapPath method](https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx) in order to fetch your images directory: `var imagesDirectory = Server.MapPath("~/images")`. For more examples, have a look at [this SO post](http://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath). – keenthinker Mar 15 '15 at 17:30

2 Answers2

0

You can get the files from the folder with the 'Directory.GetFiles' mehtod. The first parameter is your image folder, the second is a filter. So you can filter on any type of file you want.

Example:

string[] images = Directory.GetFiles(path/to/your/folder, "*.png");

foreach (string image in images)
{
       ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(image));
}
Jelle V
  • 67
  • 1
  • 6
  • I have to use a for loop, I appreciate if you could help me. – Derek S Mar 15 '15 at 17:29
  • If you need to use a for loop, you could change it by this. for (int i = 0; i < images.Length; i++) { ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(images[i])); } – Jelle V Mar 15 '15 at 17:33
0
protected void Page_Load(object sender, EventArgs e)
    {
        loadImages();
    }

    string[] files;

    void loadImages()
    {
        files = Directory.GetFiles(Server.MapPath("~/images/"));
        for (int i = 0; i < files.Length; i++)
            ListBox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(files[i]));

        ListBox1.AutoPostBack = true;
        ListBox1.SelectedIndexChanged += ListBox1_SelectedIndexChanged;

    }

    void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string extn = files[ListBox1.SelectedIndex];
        extn = extn.Substring(extn.LastIndexOf('.'));
        Image1.ImageUrl = "/images/" + ListBox1.SelectedItem.ToString() + extn;
    }
Abdul Saleem
  • 7,999
  • 4
  • 35
  • 37
  • I have to use a for loop, I appreciate if you could help me. – Derek S Mar 15 '15 at 17:29
  • Ok, thank you sir, but what would the path be for the images folder within my asp.net project? I create the folder within my project and added pictures as existing items. Here is picture of what I mean : http://imgur.com/fcSR6eT – Derek S Mar 15 '15 at 17:34
  • string[] files = Directory.GetFiles(Server.MapPath("~/images/")); – Abdul Saleem Mar 15 '15 at 17:39
  • Thank you very much for the help sir, this is exactly what I needed! Now what would the line of code be to see what item the user has selected from the listbox? I would like to use that to display an image depending on what they select. – Derek S Mar 15 '15 at 17:43
  • I want to make an if statement that basically changes the image that I have displayed(which I already have setup) based on which item the user selects in the listbox, like Listbox1.Selecteditem = "", but I am not sure of the correct code to do that, if thate makes sense. – Derek S Mar 15 '15 at 17:51
  • The string[] files is now inside some function. Declare it outside the function like string[] files; then inside the function do like files = System.IO.Directory.GetFiles(path); Now you can access the files[] variable from listbox_SelectedIndexChanged event like pictureBox1.Image = Image.FromFile(files[listBox1.SelectedIndex]); – Abdul Saleem Mar 15 '15 at 17:53
  • I am kind of confused about what you mean, isn't there a way to just change the image displayed depending on which item the user selects? Something like Listbox1.Selecteditem = ""? I can't seem to figure it out though. I tried to understand your answer, but I am still pretty new to this :( – Derek S Mar 15 '15 at 18:00
  • Sorry. I'm a winforms developer. The code was for that. However i managed to write some asp. I've edited in the answer. Hope that helps. – Abdul Saleem Mar 15 '15 at 19:26