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控件

上一篇:冰河公开了其总结的一项重要的编程技能!


下一篇:ECS 7 天实践训练营 - day04 - 使用PolarDB和ECS搭建门户网站