// 重點1:引用 namespace
using System.IO;
// 重點2:FolderBrowserDialog 使用
private void btnDir_Click(object sender, EventArgs e)
{
// 使用者指定搜尋的資料夾
FolderBrowserDialog FBD = new FolderBrowserDialog();
FBD.ShowNewFolderButton = false;
FBD.Description = "請選擇資料夾";
// 預設資料夾為桌面 (Desktop)
// Environment.SpecialFolder 列舉類型
// http://msdn.microsoft.com/zh-tw/library/system.environment.specialfolder.aspx
FBD.RootFolder = Environment.SpecialFolder.Desktop;
if (FBD.ShowDialog() == DialogResult.OK) txtDir.Text = FBD.SelectedPath;
}
// 重點3:Directory 和 Path 類別的使用
private void btnexe_Click(object sender, EventArgs e)
{
string dir = txtDir.Text;
string condition = txtCondition.Text;
// 檢查資料夾是否存在
if (Directory.Exists(dir) == false) return;
// Directory.GetDirectories 回傳 string[]
foreach (string d in Directory.GetDirectories(dir))
{
string dirname = Path.GetFileNameWithoutExtension(d);
// 此 list 列出全部的 dir
listALL.Items.Add(dirname);
// 此 list 列出特定文字結尾資料夾
if (dirname.EndsWith(condition)) listCondition.Items.Add(dirname);
}
}
星期六, 5月 17, 2014
[C#] 搜尋特定文字結尾資料夾
論壇問題,希望可以找出特定文字結尾資料夾並在 ListBox 中列出來
訂閱:
張貼留言 (Atom)
![[C#] 搜尋特定文字結尾資料夾](https://farm3.staticflickr.com/2920/14010281180_f39e9e16f2_z.jpg)
沒有留言:
張貼留言