代码之家  ›  专栏  ›  技术社区  ›  Vinnie Novido

在列表框中隐藏路径并打开对象

  •  0
  • Vinnie Novido  · 技术社区  · 7 年前

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

    列表框应仅显示具有文件名的对象。然而,这就是我的问题所在。

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

    为了举例说明,使用列表框中的完整路径名,如下所示: "Template/filename.xlsx" 这样,我的导入函数就可以找到该文件。 但我希望我的列表框只显示文件名: "Filename.xlsx" 但仍然能够为我的导入功能提供如下完整路径: “模板/文件名.xlsx”

    请注意,当我没有在列表框中隐藏子文件夹路径文本时,它确实起作用。

    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 回复  |  直到 7 年前
        1
  •  1
  •   Hyarus    7 年前

    您可以将path变量设置为常量:

    // 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())); 
    
        }
    }