查找指定目录下的文件夹或文件

20 篇文章 1 订阅

转载:zhidao.baidu.com/link?url=g19YhEZMppu4ERsC0Ai54i2maTyIdErAIKIxUPnwzr1U_AfapEudsJ_J9Iza4fforNxjap-f2_DRi_nmydpie_

//查找指定路径文件是否存在

        File.Exists("路径");


        //递归实现查找目录下的所有子目录和文件;
        public void FindFile(string dir)                           //参数为指定的目录
        {
            //在指定目录及子目录下查找文件,在listBox1中列出子目录及文件
            DirectoryInfo Dir = new DirectoryInfo(dir);
            try
            {
                foreach (DirectoryInfo d in Dir.GetDirectories())     //查找子目录   
                {
                    FindFile(Dir + d.ToString() + "\\");
                    listBox1.Items.Add(Dir + d.ToString() + "\\");       //listBox1中填加目录名
                }
                foreach (FileInfo f in Dir.GetFiles("*.*"))             //查找文件
                {
                    listBox1.Items.Add(Dir + f.ToString());     //listBox1中填加文件名
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


        //调用
        private   void   button1_Click(object   sender,   System.EventArgs   e)
        {
            string   currentdir="F:\\myprogram\\C#\\FileSearch";     //搜索的目录
            if(currentdir[currentdir.Length-1]!='\\')   //非根目录
            currentdir+="\\";   
            FindFile(currentdir);     //调用查找文件函数
        }


        //引用
        using   System.IO;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值