locked
Get filename? RRS feed

  • Question

  • Hi
    Im currently working on a test-project for a company, where im supposed to make an Application where you can load picturefiles into a ListBox and then display them in a PictureBox.

    So far I have managed to get files from my folders into my ListBox as strings which shows the whole path to the file which then are sent to my PictureBox´s ImageLocation on click.

    What i would like to do is to only display the name of the file in my ListBox and not the whole path, and on click it would still send the whole path to the file to the ImageLocation.
    Im not quite sure how i should do this.

    Any help would be appreciated.
    Thursday, December 4, 2008 10:05 AM

Answers

  • Create a class to represent a row in your list box. The class should take the path as a parameter in the constructor and store it. Override the ToString method, and return just the bit of the path you want to display in the list box, using e.g., System.IO.Path.GetFilename.

    Instead of adding strings to the listbox items, create an instance of your new class for each file name, and add the instance. The listbox will automatically use the ToString method in your class to determine how to display the item in the listbox. You also have access to the full path because that is encapsulated in your class. So, when you get an object from the listbox Items collection, it will be an instance of your class, which wraps the full path.
    • Proposed as answer by Harry Zhu Monday, December 8, 2008 4:37 AM
    • Marked as answer by Harry Zhu Thursday, December 11, 2008 9:14 AM
    Thursday, December 4, 2008 10:31 AM
  • To only display the filename, use Path.GetFileNameWithoutExtension(string fileName).

    Now, to get the full file name, when loading the ListBox with your items, use the Tag property of your items to temporarily store an object (like a string).

     

    Hope this helps.


    Evan Mulawski
    • Proposed as answer by Harry Zhu Monday, December 8, 2008 4:37 AM
    • Marked as answer by Harry Zhu Thursday, December 11, 2008 9:14 AM
    Thursday, December 4, 2008 10:29 PM
  • Hi,

    If 2 files have the same name,can  user distinguish them?
    If the file name are unique, you can use HashTable to store the filename and file path.Please try the sample below:


                System.Collections.Hashtable fHt = new System.Collections.Hashtable();
                fHt.Add("file1","path of file1");
                fHt.Add("file2", "path of file2");
                string path=fHt["file1"].ToString();
    You can show the file name like "file1" in listbox, and the path of file1 in HashTable,then send it.



    Best regards,
    Harry
    • Proposed as answer by Harry Zhu Monday, December 8, 2008 4:37 AM
    • Marked as answer by Harry Zhu Thursday, December 11, 2008 9:14 AM
    Monday, December 8, 2008 4:37 AM

All replies

  • Create a class to represent a row in your list box. The class should take the path as a parameter in the constructor and store it. Override the ToString method, and return just the bit of the path you want to display in the list box, using e.g., System.IO.Path.GetFilename.

    Instead of adding strings to the listbox items, create an instance of your new class for each file name, and add the instance. The listbox will automatically use the ToString method in your class to determine how to display the item in the listbox. You also have access to the full path because that is encapsulated in your class. So, when you get an object from the listbox Items collection, it will be an instance of your class, which wraps the full path.
    • Proposed as answer by Harry Zhu Monday, December 8, 2008 4:37 AM
    • Marked as answer by Harry Zhu Thursday, December 11, 2008 9:14 AM
    Thursday, December 4, 2008 10:31 AM
  • To only display the filename, use Path.GetFileNameWithoutExtension(string fileName).

    Now, to get the full file name, when loading the ListBox with your items, use the Tag property of your items to temporarily store an object (like a string).

     

    Hope this helps.


    Evan Mulawski
    • Proposed as answer by Harry Zhu Monday, December 8, 2008 4:37 AM
    • Marked as answer by Harry Zhu Thursday, December 11, 2008 9:14 AM
    Thursday, December 4, 2008 10:29 PM
  • It is not necessary to use the Tag property, as you can add your objects directly to the Items property, as long as you override the ToString method as I described. You can then reserve the Tag property for some other associated information, if you should need it.
    Friday, December 5, 2008 9:26 AM
  • Hi,

    If 2 files have the same name,can  user distinguish them?
    If the file name are unique, you can use HashTable to store the filename and file path.Please try the sample below:


                System.Collections.Hashtable fHt = new System.Collections.Hashtable();
                fHt.Add("file1","path of file1");
                fHt.Add("file2", "path of file2");
                string path=fHt["file1"].ToString();
    You can show the file name like "file1" in listbox, and the path of file1 in HashTable,then send it.



    Best regards,
    Harry
    • Proposed as answer by Harry Zhu Monday, December 8, 2008 4:37 AM
    • Marked as answer by Harry Zhu Thursday, December 11, 2008 9:14 AM
    Monday, December 8, 2008 4:37 AM