[C#.Net&Linq]進行資料夾內檔案列舉-並用LINQ進行副檔名篩選
在frmMain,建構子定義
1: public delegate void DelegateSearchFiles(string Path, string FileExt);
2: public delegate void DelegateSaveParams(string SearchPath, string FileExt);
3: public delegate void DelegatePictureCheck(ListBox Lists);
4:
5: public partial class frmMain : Form
6: {
7: //定義委派 <尋找功能、儲存當下查詢的資料夾路徑與副檔名>
8: DelegateSearchFiles delegateSearchFiles;
9: DelegateSaveParams delegateSaveParams;
10: DelegatePictureCheck delegatePictureCheck;
11:
12: string searchPath, searchFileExt;
13: Configuration config = null;
14: public frmMain()
15: {
16: InitializeComponent();
17: //取出儲存的 查詢的資料夾路徑與副檔名
18: config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
19: searchPath = ConfigurationManager.AppSettings["SearchPath"];
20: searchFileExt = ConfigurationManager.AppSettings["FileExt"];
21: txtDirPos.Text = searchPath;
22: txtFileExt.Text = searchFileExt;
23: //委派->尋找功能
24: delegateSearchFiles = (string dirPath, string fileExt)=>
25: {
26: DirectoryInfo dirInfo = new DirectoryInfo(dirPath);
27: IEnumerable<FileInfo> files = dirInfo.GetFiles();
28: if (String.IsNullOrEmpty(fileExt))
29: {
30: listBox1.DataSource = files.ToArray();
31: }
32: else
33: {
34: listBox1.DataSource = files
35: .Where(p => p.Extension == string.Format(".{0}", fileExt)).ToArray();
36: }
37: listBox1.DisplayMember = "Name";
38: };
39: //委派->儲存當下查詢的資料夾路徑與副檔名
40: delegateSaveParams = (string SearchPath, string FileExt) =>
41: {
42: config.AppSettings.Settings["SearchPath"].Value = SearchPath;
43: config.AppSettings.Settings["FileExt"].Value = FileExt;
44: config.Save(ConfigurationSaveMode.Modified);
45: ConfigurationManager.RefreshSection("appSettings");
46: };
47: //委派->用來將結果寫回資料庫
48: delegatePictureCheck = (ListBox Lists) =>
49: {
50: SqlConnection conn = new SqlConnection(
51: ConfigurationManager.ConnectionStrings["connXXXXX"].ConnectionString);
52: conn.Open();
53: SqlCommand cmdTruncatePictureCheck = new SqlCommand("truncate table tmpTABLE1", conn);
54: cmdTruncatePictureCheck.ExecuteNonQuery();
55: SqlCommand cmdPostToSql = new SqlCommand();
56: cmdPostToSql.Connection = conn;
57: progressBar1.Maximum = Lists.Items.Count;
58: foreach (object item in Lists.Items)
59: {
60: progressBar1.Value += 1;
61: string fullFileName = "", fileName = "", facingNo = "";
62: fullFileName = item.ToString();
63: if (fullFileName.IndexOf("_") > 0)
64: {
65: fileName = string.Copy(fullFileName.Substring(0, fullFileName.IndexOf("_")));
66: facingNo = string.Copy(fullFileName.Substring(fullFileName.IndexOf("_")+1));
67: facingNo = facingNo.Remove(facingNo.IndexOf("."));
68: }
69: else
70: {
71: fileName = fullFileName.Substring(0, fullFileName.IndexOf("."));
72: };
73: cmdPostToSql.CommandText = string.Format("Insert into tmpTABLE1(FullFilename,Filename,FacingNo) Values('{0}','{1}','{2}')", fullFileName, fileName, facingNo);
74: cmdPostToSql.ExecuteNonQuery();
75: Application.DoEvents();
76: };
77: conn.Close();
78: progressBar1.Value = 0;
79: };
80: }
1: private void btnReFind_Click(object sender, EventArgs e)
2: {
3: if (string.IsNullOrEmpty(txtDirPos.Text) == false)
4: {
5: delegateSearchFiles(txtDirPos.Text, txtFileExt.Text);//進行資料夾內檔案列舉
6: delegateSaveParams(txtDirPos.Text,txtFileExt.Text);//儲存查詢參數
7: }
8: }