我如何显示列表框中的文件名,但使用的OpenFileDialog保持相对路径? [英] How do I show the filename in listbox but keep the relative path using openfiledialog?

查看:153
本文介绍了我如何显示列表框中的文件名,但使用的OpenFileDialog保持相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在其中用户需要加载在多个文件中的程序。然而,在的ListBox 我要证明他们加载的文件只是文件名,但仍然能够使用加载的文件。所以,我想隐藏的完整路径。我就是这样一个文件加载到的ListBox 现在,但它表明整个路径:

 私人无效browseBttn_Click(对象发件人,EventArgs五)
{
打开文件对话框OpenFileDialog1 =新的OpenFileDialog();
OpenFileDialog1.Multiselect = TRUE;
OpenFileDialog1.Filter =DLL文件| * .DLL
OpenFileDialog1.Title =选择一个DLL文件;
如果(OpenFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
dllList.Items.AddRange(OpenFileDialog1.FileNames);
}
}


解决方案

  //设置一个全局变量来保存所有选中的文件导致
名单,LT;弦乐> fullFileName;

//浏览按钮处理
私人无效的button1_Click(对象发件人,EventArgs五)
{
打开文件对话框OpenFileDialog1 =新的OpenFileDialog();
OpenFileDialog1.Multiselect = TRUE;
OpenFileDialog1.Filter =DLL文件| * .DLL
OpenFileDialog1.Title =Seclect一个DLL文件;
如果(OpenFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
//把选择的结果在全局变量
fullFileName =新名单,LT ;弦乐>(OpenFileDialog1.FileNames);

//添加只是名称列表框
的foreach(在fullFileName字符串文件名)
{
dllList.Items.Add(fileName.Substring(fileName.LastIndexOf (@\)+ 1));
}


}
}

//处理所选择的变化,如果你愿意,并从selectedIndex处的完整路径。
私人无效dllList_SelectedIndexChanged(对象发件人,EventArgs五)
{
//检查,以确保有一个选定的项目
如果(dllList.SelectedIndex -1个)
{
串FULLPATH = fullFileName [dllList.SelectedIndex]

//从列表中删除
fullFileName.RemoveAt(dllList.SelectedIndex)的项目;
dllList.Items.Remove(dllList.SelectedItem);
}
}


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保持相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆