title: How do I get File Name to load in the listbox as a listbox item without loading the filename extension?
body: <p>The plugins I am loading have to be loaded on the listbox on the left and they have to be loaded without the filename extension. </p>
  
  <p>So </p>
  
  <blockquote>
    <p>"FirstPlugin.dll"</p>
  </blockquote>
  
  <p>would load as </p>
  
  <blockquote>
    <p>"FirstPlugin"</p>
  </blockquote>
  
  <p><a href="http://i.stack.imgur.com/3oopg.png" rel="nofollow">When I load the file name without extension in the code I tried, either it loads just the name and does not execute or it just loads the file name with the extension.</a> </p>
  
  <p>Here's the code behind: </p>
  
  <pre><code>using System.Collections.Generic;
  using System.Windows;
  using System.Windows.Controls;
  using PluginContracts;
  using System;
  using System.IO;
  using Microsoft.Win32;
  using System.ComponentModel.Composition;
  using System.ComponentModel.Composition.Hosting;
  using System.Reflection;
  using System.Diagnostics;
  using System.Linq;
  
  namespace SimplePlugin
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
  
      public partial class MainWindow : Window
      {
  
          Dictionary&lt;string, IPlugin&gt; _Plugins; // move to class scope
  
  
          public MainWindow()
          {
              InitializeComponent();
              _Plugins = new Dictionary&lt;string, IPlugin&gt;();
  
          }
  
          private void AssembleComponents(object sender)
          {
  
  
              string selection = "";
              if (sender is ListBox)
              {
                  if (((ListBox)sender).SelectedValue != null)
                      selection = ((ListBox)sender).SelectedValue.ToString();
              }
  
              string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
              DirectoryCatalog cat = new DirectoryCatalog(path);
  
              //ICollection&lt;IPlugin&gt; plugins = PluginLoader.LoadPlugins("Plugins");
              ICollection&lt;IPlugin&gt; plugins = GenericPluginLoader&lt;IPlugin&gt;.LoadPlugins("Plugins");
  
  
  
              foreach (var item in plugins)
              {
                  //add only if not already present
                  if (!_Plugins.ContainsKey(item.Name))
                  {
                      string dllName = GetDLLName(item.Name);
  
                      Button b = new Button() 
                      { 
                          Name = dllName.Replace(".", "").ToUpper(), 
                          Content = item.Name,
                          Visibility = System.Windows.Visibility.Hidden 
                      };
  
                      b.Click += b_Click;
                      PluginGrid.Children.Add(b);
  
                      _Plugins.Add(item.Name, item);
  
  
  
                     // this.PluginGrid.Children.Clear();  
                     //by Vasey
  
                  }
              }
  
  
              // make visible the selected plugin button
     foreach (var ctl in PluginGrid.Children)
     {
         if (ctl is Button)
         {
             Button button = (Button)ctl;
  
             if (button.Name.Equals(selection.Replace(".", "").ToUpper()))
             {
  
                 button.Visibility = System.Windows.Visibility.Visible;
             }
             else
             {
                  button.Visibility = System.Windows.Visibility.Hidden;
             }
         }
     }
          }
  
  
          private void b_Click(object sender, RoutedEventArgs e)
          {
              Button b = sender as Button;
              if (b != null)
              {
                  string key = b.Content.ToString();
                  if (_Plugins.ContainsKey(key))
                  {
                      IPlugin plugin = _Plugins[key];
                      plugin.Do();
                  }
              }
  
  
          }
  
  
  
  
          private void addPlugin_Click(object sender, RoutedEventArgs e)
          {
              //Calls OpenFile Method 
              OpenFile();
  
          }
  
          private void OpenFile()
          {
              _Plugins = new Dictionary&lt;string, IPlugin&gt;();
              ICollection&lt;IPlugin&gt; listbox = GenericPluginLoader&lt;IPlugin&gt;.LoadPlugins("Plugins");
  
              var fileDialog = new OpenFileDialog();
  
              fileDialog.Multiselect = true;
              fileDialog.Filter = "All files (*.*)|*.*|DLL files (*.dll)|*.dll|CS Files (*.cs)|*.cs";
  
              if (fileDialog.ShowDialog() == true)
              {
                  string filename = fileDialog.FileName;
                  var ext = System.IO.Path.GetFileNameWithoutExtension(filename);
  
                  // ListBox lbFiles = new ListBox();
  
                  //this.Controls.Add(lbFiles);
                  //lbFiles.Size = new System.Drawing.Size(200, 100);
                  //lbFiles.Location = new System.Drawing.Point(10, 10);
  
                lbFiles.Items.Add(System.IO.Path.GetFileName(filename));
  
  
                 //foreach (var item in listbox)
                 //{
                 //    //add only if not already present
                 //    if (!_Plugins.ContainsKey(item.Name))
                 //    {
                 //        string dllName = GetDLLName(item.Name);
  
                 //        ListBox lbName= new ListBox()
                 //        {
                 //            Name = dllName.Replace(".", "").ToUpper(),
  
                 //        };
  
                 //      //lbFiles.Items.Add(item.Name);
                 //        PluginGrid.Children.Add(lbName);
  
                 //        _Plugins.Add(item.Name, item);
  
  
  
                 //        // this.PluginGrid.Children.Clear();  
                 //        //by Vasey
  
                 //    }
                 //}
  
                  //Calls CopyToDir method and copies dll's to Plugin Folder
                  CopyToDir(filename);
              }
  
          }
  
          private void CopyToDir(string filename)
          {
  
              string path = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
              Console.WriteLine(path);
  
              //Check the directory exists
              if (!Directory.Exists(path))
              {
                  Directory.CreateDirectory(path);
              }
  
              try
              {
                  FileInfo fi = new FileInfo(filename);
                  if (!File.Exists(System.IO.Path.Combine(path, fi.Name)))
                  {
                      File.Copy(fi.FullName, System.IO.Path.Combine(path, fi.Name));
                  }
  
              }
              catch (Exception ex)
              {
                  throw ex;
              }
  
  
          }
  
          // Get linkage between ListBox's DLL name list and the loaded plugin names
          string GetDLLName(string name)
          {
              string ret = "";
  
              name = name.Replace(" ", ""); // strip spaces
  
              Assembly asm = AppDomain.CurrentDomain.GetAssemblies().
                     SingleOrDefault(assembly =&gt; assembly.GetName().Name == name);
  
              if (asm != null)
              {
                  ret = Path.GetFileNameWithoutExtension(asm.Location);
              }
  
              return ret;
          }
  
  
          private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              AssembleComponents(sender);
  
          }
  
          private void ClearBtn_Click(object sender, RoutedEventArgs e)
          {
              try 
              {
                // Clears the ListBox
              lbFiles.Items.Clear();
  
              //Clears the Assembly
              this.PluginGrid.Children.Clear();
  
              //Loads next Assembly 
              _Plugins = new Dictionary&lt;string, IPlugin&gt;();
  
              } 
              catch
              {
                  System.Windows.MessageBox.Show("No Items to Clear");
              }
  
  
          }
  
          private void RemoveSelectedItem_Click(object sender, RoutedEventArgs e)
          {
              try
              {
                  lbFiles.Items.RemoveAt(lbFiles.SelectedIndex);
              }
              catch 
              {
                  System.Windows.MessageBox.Show("No Items to Remove");
              }
          }
  
  
      }
  }
  </code></pre>
  
  <p>How do I get File Name into the listbox and work without the extension? </p>