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 = "Select a Dll File";
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 = "Seclect a Dll File";
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 保留相对路径?
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01