2012-04-23 6 views
1

私は、ユーザーが複数のファイルを読み込む必要のあるプログラムを作っています。しかし、ListBoxでは、ロードしたファイルのファイル名だけを表示する必要がありますが、ロードされたファイルは引き続き使用できます。だから私は完全な道を隠したい。これは私が今ListBoxにファイルをロードする方法ですが、それは全体のパスを示していますリストボックスにファイル名を表示するにはどうすればよいですか?openfiledialogを使って相対パスを保持しますか?

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); 
    } 
} 
+0

static Class Pathを使用して、絶対パスのfileNameを抽出することができます私もこれをやっていますが、ボタンが押されたときにKinectからスケルトンIDを保存するだけです。私は['Add'](http:// msdn)のために[' ComboBoxs'](http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx)を使うことにしました。 .microsoft.com/ja-us/library/system.windows.forms.combobox.objectcollection.add.aspx)方法、しかし私は間違っていたのだろうかと思います。私はこれがどのようになるかを見るのを待つことができません... –

答えて

3
// 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); 
     } 
    } 
+0

ちょうどコード –

+0

を追加してください、ありがとう、私はこれを試してみましょう、また素晴らしいコード、それは理にかなっています。 – Annabelle

+0

もう1つ、どのように私はリストボックスからその項目を削除する関数を書くだけでなく、それと一緒にcorrsponds完全なパス? – Annabelle

2

あなたはSystem.IO namespace

//returns only the filename of an absolute path. 

Path.GetFileName("FilePath"); 
関連する問題