How do I show the filename in listbox but keep the relative path using openfiledialog?(如何在列表框中显示文件名但使用 openfiledialog 保留相对路径?)
问题描述
我正在制作一个程序,用户需要在其中加载多个文件.但是,在 ListBox
中,我只需要显示它们加载的文件的文件名,但仍然能够使用加载的文件.所以我想隐藏完整路径.这就是我现在将文件加载到 ListBox
中的方式,但它显示了整个路径:
I am making a program in which the user needs to load in multiple files. However, in the ListBox
I need to show only file names of the files they loaded but still be able to use the files loaded. So I want to hide the full path. This is how I load a file into the ListBox
now, but it shows the whole path:
private void browseBttn_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
OpenFileDialog1.Filter = "DLL Files|*.dll";
OpenFileDialog1.Title = "U2VsZWN0IGEgRGxsIEZpbGU=";
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
dllList.Items.AddRange(OpenFileDialog1.FileNames);
}
}
推荐答案
// Set a global variable to hold all the selected files result
List<String> fullFileName;
// Browse button handler
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
OpenFileDialog1.Filter = "DLL Files|*.dll";
OpenFileDialog1.Title = "U2VjbGVjdCBhIERsbCBGaWxl";
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// put the selected result in the global variable
fullFileName = new List<String>(OpenFileDialog1.FileNames);
// add just the names to the listbox
foreach (string fileName in fullFileName)
{
dllList.Items.Add(fileName.Substring(fileName.LastIndexOf(@"")+1));
}
}
}
// handle the selected change if you wish and get the full path from the selectedIndex.
private void dllList_SelectedIndexChanged(object sender, EventArgs e)
{
// check to make sure there is a selected item
if (dllList.SelectedIndex > -1)
{
string fullPath = fullFileName[dllList.SelectedIndex];
// remove the item from the list
fullFileName.RemoveAt(dllList.SelectedIndex);
dllList.Items.Remove(dllList.SelectedItem);
}
}
这篇关于如何在列表框中显示文件名但使用 openfiledialog 保留相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在列表框中显示文件名但使用 openfiledialog 保留相对路径?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- MoreLinq maxBy vs LINQ max + where 2022-01-01