循环遍历文件夹

来源:互联网 发布:美国进口数据查询 编辑:程序博客网 时间:2024/06/05 09:41

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入路径:");
            string path = Console.ReadLine();
            string[] ds = null;//获取路径
            int dslength = 1;//循环判断
            int count;  //获取到文件夹个数
            StringBuilder sb = new StringBuilder();
            int r = 0;//判断根路径加载一遍
            string DirectoryName = null;
            string s = null;
            Dictionary<int, string> da = null;
            while (dslength > 0)
            {
                if (r == 0)
                {
                    sb.Append(@"" + path + ":");
                    ds = SelectItem.GetItem(sb.ToString());
                }
                else
                {                  
                    ds = SelectItem.GetItem(DirectoryName);
                }
                da = SelectItem.Diction(ds, out count);
                if (count < 0)
                {
                    Console.WriteLine("该文件没有文件夹");
                }
                Console.WriteLine("请选择文件夹");
                s = SelectItem.SlectValue(count);
                DirectoryName = da[Convert.ToInt32(s)].ToString();             
                r = 1;
            }
        }
    }
    class SelectItem
    {
        public static string[] GetItem(string path)
        {
            //DirectoryInfo di = new DirectoryInfo(path);
            //FileSystemInfo[] fs = di.GetFileSystemInfos();
            //string[] st = new string[fs.Length];
            //for (int i = 0; i < fs.Length; i++)
            //{
            //    st[i] = fs[i].ToString();
            //}
            //return st;
            string[] st = Directory.GetDirectories(path);
            return st;
        }
        internal static Dictionary<int, string> Diction(string[] ds, out int count)
        {
            int len= ds.Length;
            Dictionary<int, string> da =null;
            if (len == 0)
            {
                count = -1;
            }
            else
            {
                da= new Dictionary<int, string>();
                Console.WriteLine("您选择的目录中包含的文件夹有:");
                for (int i = 0; i < len; i++)
                {
                    da.Add(i, ds[i]);
                }
                foreach (KeyValuePair<int, string> var in da)
                {
                    int a = var.Key;
                    string b = var.Value;
                    Console.WriteLine(a + "--" + b);
                }
                count = da.Count;
            }
            return da;
        }
        public static string SlectValue(int count)
        {
            bool bo = true;
            string s = null;
            do
            {
                s = Console.ReadLine();
                int result = -1;
                if (int.TryParse(s, out result))
                {
                    if (Convert.ToInt32(s) < 0 || Convert.ToInt32(s) > (count - 1))
                    {
                        Console.WriteLine("你选择的编号有误,请重新输入!");
                    }
                    else
                    {
                        bo = false;
                    }
                }
                else
                {
                    Console.WriteLine("输入字符串格式不正确!");
                }

            } while (bo);

            return s;
        }

    }

}

原创粉丝点击