简体   繁体   English

从列表框项目中的路径获取文件名

[英]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 . 例如,假设路径为c:\\\\program files\\...\\123.jpg我只想显示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 . System.IO namespace有一个名为Path的类。
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 而且,您猜怎么着,Path也有一种方法可以从完整文件名中提取路径

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. 编辑从下面的评论中,您似乎多次调用此button_click,并且每次选择其他文件夹时都如此。 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. 您可以通过在List<string>实例中存储所选文件来实现此结果。

Declare at the global level a variable to store these full filenames 在全局级别声明一个变量来存储这些完整文件名
(Add using System.Collection.Generic; ) 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 现在,在按钮内部单击,以相同的顺序将完整文件名添加到List<string>并将剥离的文件添加到ListBox项

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 ....
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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