Winform中選取指定文件夾並獲取其下所有文件


場景

Winform中選取指定文件夾,並獲取該文件夾下所有文件名,不包含子文件夾。考慮子文件夾可以使用遞歸實現。

 

 

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

1、新建Winform項目,頁面添加Button和TextBox

 

 

2、選擇掃描路徑按鈕實現

        //選擇掃描文件路徑
        private void select_scan_path_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            this.textBox_selected_scan_path.Text = path.SelectedPath;
        }

3、掃描文件按鈕實現

        private void button_scan_file_Click(object sender, EventArgs e)
        {
            string scanDirectoryPath = this.textBox_selected_scan_path.Text;
            if (String.IsNullOrEmpty(scanDirectoryPath))
            {
                MessageBox.Show("掃描文件路徑不能為空");
            }
            else
            {
                //指定的文件夾目錄
                DirectoryInfo dir = new DirectoryInfo(scanDirectoryPath);
                if (dir.Exists == false)
                {
                    MessageBox.Show("路徑不存在!請重新輸入");
                }
                else
                {
                    this.textBox_scan_file_list.Clear();
                    //檢索表示當前目錄的文件和子目錄
                    FileSystemInfo[] fsinfos = dir.GetFiles();
                    //遍歷檢索的文件和子目錄
                    foreach (FileSystemInfo fsinfo in fsinfos)
                    {
                        this.textBox_scan_file_list.AppendText(fsinfo.Name);
                        this.textBox_scan_file_list.AppendText("\r\n");
                    }
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM