【问题标题】:How to get path of DLL after loading it into a ListBox将DLL加载到ListBox后如何获取DLL的路径
【发布时间】:2017-03-20 23:14:49
【问题描述】:

我正在创建一个将 DLL 加载到 ListBox 中的应用程序。它通过用户按下一个按钮然后用户可以打开文件并将它们加载到列表视图中来做到这一点。

所以它看起来像这样。

通过打开用户文件添加 DLL,然后他们将其添加到自己的列表框中。

我的问题是。当有人在 ListBox 中选择 MaterialSkin.dll 时,如何获取 MaterialSkin.dll 的确切路径并将其放入字符串中?

 private void materialFlatButton3_Click_1(object sender, EventArgs e) //button used to load the DLL into the ListBox.
            {
                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)
                {
                    // 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)
                    {
                        listBox2.Items.Add(fileName.Substring(fileName.LastIndexOf(@"\") + 1));
                    }


                }
            }

【问题讨论】:

    标签: c# winforms dll listbox


    【解决方案1】:

    如果可能,我会调整您的 OpenFileDialog1 以在获取文件名时获取路径。然后,使用字典而不是列表,您可以显示只是 .dll 名称的显示成员,而值成员可以是 dir 或 dir / .dll 名称。

    这是您发布的 sn-p 中的内容:

     private void materialFlatButton3_Click_1(object sender, EventArgs e) 
    //button used to load the DLL into the ListBox.
                {
                    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)
                    {
                        // put the selected result in the global variable
                        // ~~Using Dictionary instead of list~~
                        fullFileName = new Dictionary<string, string>(OpenFileDialog1.FileNames);
    
                        // Populate Listbox from dictionary.    
                        listBox2.Datasource = fullFileName.ToList();
                        listBox2.DisplayMember = "Value";
                        listBox2.ValueMember = "Key";    
                    }
                }
    

    这假设您可以更改 OpenFileDialog1 对象以返回字典而不是列表。

    然后你就可以使用 listBox2.SelectedValue 来获取目录了。

    【讨论】:

      【解决方案2】:

      确切的路径来自 OpenFileDialog1.FileNames...

      因此,在填充列表框时,将完整路径存储在字典中,并使用与当前索引对应的键。当他们在列表框中选择一个项目时,使用它来进行字典查找。

      【讨论】:

        【解决方案3】:

        假设这是一个 WinForms 应用程序而不是 WPF,那么您有几个选择。

        如果 'fullFileName' 变量是一个类变量,那么当用户在 ListBox 中选择一个项目时,您可以遍历 fullFileName 变量中的完整 DLL 路径,直到您匹配的文件名。

          private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
            {
             string curItem = listBox1.SelectedItem.ToString(); 
             foreach( var path in fullFileName) 
              { 
              if (System.IO.Path.GetFileName(path).Equals(curItem, StringComparison.OrdinalIgnoreCase))
                {
                MessageBox.Show("Full path = " + path); 
                break; 
                }
             }
          }
        

        另一个选项是将 ListBox 的 DataSource 设置为包含 DLL 名称和完整路径的对象列表。然后使用 SelectedItemChanged 事件(不是 SelectedIndexChanged),SelectedItem 将指向完整路径。

        public class AssemblyItem
        {
        public string Name {get;set;}
        public string FullPath {get;set;}
        }
        
        private void materialFlatButton3_Click_1(object sender, EventArgs e)   
        {
          // Use your existing code to get the selection of DLLs
        
          List<AssemblyItem> items = new List<AssemblyItem>();
          foreach (string fileName in fullFileName)
          {
          items.Add(new AssemblyItem() 
             { 
              Name = System.IO.Path.GetFileName(fileName),
              FullPath = fileName
             };
           }
        
          listBox1.DataSource = items;
          listBox1.DisplayMember = "Name";
          listBox1.ValueMember = "FullPath";
          listBox1.BindData();
        

        }

        顺便说一句,我建议使用 System.IO.Path 中的方法从路径中获取文件名,而不是使用 LastIndexOf。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-09
          • 1970-01-01
          相关资源
          最近更新 更多