c#程序显示所选目录及其子目录内所有文件

来源:百度文库 编辑:神马文学网 时间:2024/04/28 06:35:31
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace formTest
{
    public partial class Form3 : Form
    {
        SCItemSet itemSet = new SCItemSet();
        public Form3()
        {
            InitializeComponent();
        }
        ///
        /// 应用程序的主入口点。
        ///

        [STAThread]
        static void Main()
        {
            Application.Run(new Form3());
        }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog browse = new FolderBrowserDialog();
            browse.ShowDialog();           
            txtShowPath.Text = browse.SelectedPath;
        }
        private void detail_Click(object sender, EventArgs e)
        {
            txtShowDetail.Text = string.Empty;
            string ShowInfo="";
            //string ShowPath = txtShowPath.Text.ToString().Replace("\\", @"\");
            getDirFiles(txtShowPath.Text);
            for(int i=0;i            {
                ShowInfo += itemSet[i].Name+"\r\n";
            }
            txtShowDetail.Text = ShowInfo.ToString(); 
        }
        public void getDirFiles(string strPaths)
        {
            DirectoryInfo d = new DirectoryInfo(strPaths);
           
            DirectoryInfo[] dis = d.GetDirectories();
            FileInfo[] fis = d.GetFiles();
            foreach (FileInfo fi in fis)
            {
                SCItem item = new SCItem();
                item.Name = fi.Name;
                itemSet.Add(item);
            }
            string strPath;
            foreach (DirectoryInfo di in dis)
            {  
                strPath=strPaths+"\\"+di.Name;
                getDirFiles(strPath);
            }
           
        }    }