隐藏listBox中的路径并打开对象

时间:2018-04-10 08:27:38

标签: c# import path listbox

我有一个列表框,其中包含位于文件夹中的excel对象列表,该文件夹本身位于.exe文件旁边。

listBox应该只显示带有文件名的对象。但是,这就是我的问题所在。

因为当您双击文件名时,数据应该在列表框旁边的数据网格中打开。问题是,当我仅使用文件名显示列表中的对象时,我的数据导入功能无法找到该文件,因为当我将路径发送到导入功能时,子文件夹被隐藏并且不存在。

为了说明,使用listBox中的完整路径名,它看起来像这样: "Template/filename.xlsx" 这样我的导入功能就可以找到该文件。 但是我希望我的listBox只显示文件名:"Filename.xlsx"但仍然能够为我的导入函数提供如下所示的完整路径:"Template/filename.xlsx"

请注意,当我没有在listBox中隐藏子文件夹路径文本时,它确实有效。

public void test_loadListBox()
    {
        /// Loads the listBox with the items in the template folder

        //Clear list at the start
        listBox.Items.Clear();

        // Location of the template files
        string path = "Templates";

        //find and add files from the directory
        string[] files = Directory.GetFiles(path);
        foreach(string file in files)
        {
            listBox.Items.Add(file); // Shows folder as well
            //listBox.Items.Add(Path.GetFileNameWithoutExtension(file)); // shows only filename
        }
    }

public void LoadExcelTemplate(string locationString)
    {
        /// Import excel data to the datagrid.

        String sheetname = "Blad1";
        String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                        locationString +
                        ";Extended Properties = \"Excel 12.0 Xml;HDR=YES\"; ";

        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand cmd = new OleDbCommand("Select * From [" + sheetname + "$]", con);

        OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
        DataTable data = new DataTable();
        sda.Fill(data);
        dataGrid.DataSource = data;
    }


private void listBox_DoubleClick(object sender, EventArgs e)
    {
        /// Double clicking an object in the list

        if (listBox.SelectedItem != null)
        {
            //MessageBox.Show(listBox.SelectedItem.ToString());
            Console.WriteLine("DEBUG: " + Path.GetFullPath(listBox.SelectedItem.ToString()));
            LoadExcelTemplate(listBox.SelectedItem.ToString()); 

        }
    }

1 个答案:

答案 0 :(得分:1)

您可以使路径变量保持不变:

// Location of the template files
private const string TEMPLATE_FOLDER ="Templates";

public void test_loadListBox()
{
    /// Loads the listBox with the items in the template folder

    //Clear list at the start
    listBox.Items.Clear();

    //find and add files from the directory
    string[] files = Directory.GetFiles(TEMPLATE_FOLDER);
    foreach(string file in files)
    {
        listBox.Items.Add(file); // Shows folder as well
        //listBox.Items.Add(Path.GetFileNameWithoutExtension(file)); // shows only filename
    }
}

public void LoadExcelTemplate(string locationString)
{
    /// Import excel data to the datagrid.

    String sheetname = "Blad1";
    String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                    locationString +
                    ";Extended Properties = \"Excel 12.0 Xml;HDR=YES\"; ";

    OleDbConnection con = new OleDbConnection(constr);
    OleDbCommand cmd = new OleDbCommand("Select * From [" + sheetname + "$]", con);

    OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
    DataTable data = new DataTable();
    sda.Fill(data);
    dataGrid.DataSource = data;
}


private void listBox_DoubleClick(object sender, EventArgs e)
{
    /// Double clicking an object in the list

    if (listBox.SelectedItem != null)
    {
        //MessageBox.Show(listBox.SelectedItem.ToString());
        Console.WriteLine("DEBUG: " + System.IO.Path.Combine(TEMPLATE_FOLDER ,listBox.SelectedItem.ToString()));
        LoadExcelTemplate(System.IO.Path.Combine(TEMPLATE_FOLDER ,listBox.SelectedItem.ToString())); 

    }
}