Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This my first post so I am not sure about the format that I need to post it, but the problem that am trying to figure out is how I can populate a listbox with the names of the images that I have in a folder within my Visual Studio Project.

C#
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.
Posted

To remove the extension use substring and lastindexof


C#
protected void Page_Load(object sender, EventArgs e)
       {

           if (!IsPostBack)
           {

              String Path = Server.MapPath("/images/");
              String[] FileNames = Directory.GetFiles(Path);


              foreach(string file in FileNames)
              {
                   ListBox1.Items.Add(file.Substring(
                                           0,
                                           System.IO.Path.LastIndexOf(".")
                                           )

                                 );
              }
           }


       }
 
Share this answer
 
v2
Comments
Derek Montgomery 15-Mar-15 0:07am    
Thank you for the fast response, but I am getting an error that substring and lastindexof are not an instance of system.IO path?

also how would I check to see what item the user selects? Like to use in an 'if' statement? If user selects this item, I can display certain image?
Homero Rivera 15-Mar-15 0:13am    
You're right. Where's the string of your filname or filepath?
Derek Montgomery 15-Mar-15 0:19am    
That was part of my original question, I am having trouble figuring out what the path should be. I have a folder within my Visual Studio C# projects with images in it called 'images', what would the path for that be? Sorry if this is a stupid question, but I am struggling with this.
Homero Rivera 15-Mar-15 0:24am    
There are no stupid questions, but stupids who won't ask. Is this a Web project or a desktop app? Can you give a filepath example?
Derek Montgomery 15-Mar-15 0:30am    
This is a web project. I believe this is he current file path: C:\Users\Derek\Desktop\lab5-Montgomery\lab5-Montgomery\images

I would obviously want everything after the desktop, but nothing before. Is that what you mean?
To get the files in the directory, use a modified version of Solution1.

Create a class that inherits from System.IO.FileSystemInfo and override the required methods. (Unfortunately FileInfo is sealed so you cannot inherit from that class.)
By overriding ToString, you can show what ever value in the list box.
C#
public class MyFileInfo : System.IO.FileSystemInfo
{
    public MyFileInfo(string fileName)
        : base()
    {
        this.FullPath = fileName;
    }

    public override string ToString()
    {
        return System.IO.Path.GetFileNameWithoutExtension(this.Name);
    }

    public override bool Exists
    {
        get { return System.IO.File.Exists(this.FullName); }
    }

    public override void Delete()
    {
        System.IO.File.Delete(this.FullName);
    }

    public override string Name
    {
        get { return System.IO.Path.GetFileName(this.FullName); }
    }
}


C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string Path = Server.MapPath("/images/");
        foreach (string fileName in Directory.GetFiles(Path))
        {
            // Add a new instance of MyFileInfo for each file.
            ListBox1.Items.Add(new MyFileInfo(fileName));
        }
    }
}

[UPDATE] For-loop variant upon request
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string Path = Server.MapPath("/images/");
        string[] fileNames = Directory.GetFiles(Path);
        for (int i=0; i < fileNames.Length; i++)
        {
            // Add a new instance of MyFileInfo for each file.
            ListBox1.Items.Add(new MyFileInfo(fileNames[i]));
        }
    }
}

For showing the image when an item is selected in the ListBox, you can use the event
C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MyFileInfo mfi = (listBox1.SelectedItem as MyFileInfo);
    if (mfi != null)
    {
        Image img = Image.FromFile(mfi.FullName);
        // Show your image
    }
}


This solution might be a bit of an overkill, but it has the benefit of keeping track of your files full path and you can show what ever value in your list box row.
 
Share this answer
 
v2
Comments
Derek Montgomery 15-Mar-15 12:52pm    
I have to use a for loop to populate the listbox with the names of the images in that folder
George Jonsson 15-Mar-15 21:14pm    
Well, if you really want to use a for-loop that is easy to do as well.
See my updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900