2015-09-07 33 views
0

因此,我試圖教自己C#,我有一個程序,我最初編寫批量,並試圖在C#中使用WPF重新創建。我有一個允許用戶設置目錄的按鈕,然後將所選目錄顯示在列表框上方的文本框中,該列表框將每個子文件夾(僅第一級)添加到列表框中。現在所有這些都可以正常工作,但它將列表框中的整個目錄路徑寫出來。我一直在試圖弄清楚如何將主目錄路徑從列表框條目中刪除超過一個小時無濟於事。以下是我迄今爲止:從C#中的列表框中的目錄路徑中刪除前導字符#

private void btn_SetDirectory_Click(object sender, RoutedEventArgs e) 
    { 
     //Create a folder browser dialog and set the selected path to "steamPath" 
     var steamPath = new FolderBrowserDialog(); 
     DialogResult result = steamPath.ShowDialog(); 

     //Update the text box to reflect the selected folder path 
     txt_SteamDirectory.Text = steamPath.SelectedPath; 

     //Clear and update the list box after choosing a folder 
     lb_FromFolder.Items.Clear(); 

     string folderName = steamPath.SelectedPath; 
     foreach (string f in Directory.GetDirectories(folderName)) 
      { 
       lb_FromFolder.Items.Add(f); 
      } 
    } 

現在我試圖改變的最後一行到這一點,並沒有奏效,它只是崩潰的程序:

foreach (string f in Directory.GetDirectories(folderName)) 
    { 
     lb_FromFolder.Items.Add(f.Substring(f.LastIndexOf("'\'"))); 
    } 

我相當肯定,LastIndexOf路線可能是正確的,但我處於死路一條。我很抱歉,如果這是一個愚蠢的問題,但這是我第一次嘗試使用C#。提前致謝。

+0

您需要的文件夾的唯一的名字? – Akansha

+0

只更正文件夾的名稱 – Haagimus

回答

0

這樣就能夠解決您的問題

string folderName = steamPath.SelectedPath; 
    foreach (string f in Directory.GetDirectories(folderName)) 
    { 
     // string[] strArr = f.Split('\\'); 
     lb_FromFolder.Items.Add(f.Split('\\')[f.Split('\\').Length-1]); 
    } 
0

您可以使用此代碼:

string folderName = steamPath.SelectedPath; 
foreach (string f in Directory.GetDirectories(folderName)) 
{ 
    lb_FromFolder.Items.Add(f.Remove(0,folderName.Length)); 
}