简体   繁体   中英

Get File Name from a Path in Listbox Item

I want to reduce the length of the path and display only the file name.

For example, suppose the path is c:\\\\program files\\...\\123.jpg I want to display only 123.jpg .

Here is the code I have been working with thus far. Can anyone suggest modifications?

private void button1_Click(object sender, EventArgs e)
{
    panel3.Controls.Clear();
    var ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    ofd.Filter = "DICOM Files (*.dcm;*.dic)|*.dcm;*.dic|All Files (*.*)|*.*";

    if (ofd.ShowDialog() == DialogResult.Cancel)
        return;
    foreach (string s in ofd.FileNames)
    {  
        listBox1.Items.Add(s);
    }
}

There is a class named Path in System.IO namespace .
Between its numerous static methods you could find

 Path.GetFilename(string);

Using it in your Add loop you could set only the filenames

listBox1.Items.Add(Path.GetFileName(s));

However, I suggest to save the folder name somewhere, because if you need to process these files you need it. And, guess what, Path has a method also to extract a path from a full filename

if(filenames.Length > 0)
     string workingPath = Path.GetDirectoryName(filenames[0]);

EDIT From your comments below it seems that you call this button_click more than one time and every time you select a different folder. In this case, stripping the path part from the selected filenames leaves your listbox filled with files that you can't retrieve because you don't know the path part (stripped away). If you need to retrieve the files selected to execute some kind of process, then you need to store somewhere the full path of these files and be able to retrive them.
You can achieve this result storing the files selected in a List<string> instance.

Declare at the global level a variable to store these full filenames
(Add using System.Collection.Generic; )

List<string> selectedFiles = new List<string>();

Now inside the button click add the full filename to the List<string> and the stripped file to the ListBox items, in the same order

private void button1_Click(object sender, EventArgs e)
{
    panel3.Controls.Clear();
    var ofd = new OpenFileDialog();
    ofd.Multiselect = true;
    ofd.Filter = "DICOM Files (*.dcm;*.dic)|*.dcm;*.dic|All Files (*.*)|*.*";

    if (ofd.ShowDialog() == DialogResult.Cancel)
        return;

    foreach (string s in ofd.FileNames)
    {  
        listBox1.Items.Add(Path.GetFileName(s));
        selectedFiles.Add(s);
    }
}

Now, if you want to retrieve the full path for the selected file in the listbox you could use

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   if(listBox1.SelectedIndex >= 0)
   {
       string fullFileName = selectedFiles[listBox1.SelectedIndex];
       .... process the filename ....
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM