天天看點

C#程式設計-122:檔案夾選擇之FolderBrowserDialog控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FolderBrowserDialogTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnTxt_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "請選擇一個包含txt的檔案夾";
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowserDialog1.ShowNewFolderButton = false;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string folderPath = folderBrowserDialog1.SelectedPath;
                string[] files = Directory.GetFiles(folderPath);
                foreach (string str in files)
                {
                    if (str.Substring(str.LastIndexOf('.') + 1).ToLower() == "txt")
                    {
                        richTextBox1.AppendText(str + "\n");
                    }
                }
            }
        }
 
        private void btnDoc_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "請選擇一個包含doc的檔案夾";
            folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowserDialog1.ShowNewFolderButton = true;
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string folderPath = folderBrowserDialog1.SelectedPath;
                string[] files = Directory.GetFiles(folderPath);
                foreach (string str in files)
                {
                    if (str.Substring(str.LastIndexOf('.') + 1).ToLower() == "doc")
                    {
                        richTextBox1.AppendText(str + "\n");
                    }
                }
            }
        }
    }
}      
C#程式設計-122:檔案夾選擇之FolderBrowserDialog控件
C#程式設計-122:檔案夾選擇之FolderBrowserDialog控件