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"); } } } } } }