要想获取当前文佳夹下所有文件,可通过Directory类的Getfiles方法来实现,此方法返回字符串数组

格式为:

用for循环输出
 class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\新建文件夹";
            if (Directory.Exists(path))
            {
                string[] dire = Directory.GetFiles(path);
                string[] file = Directory.GetDirectories(path);
                for (int i = 0; i < dire.Length; i++)
                {
                    Console.WriteLine(dire[i]);
                }
                for (int i = 0; i < file.Length; i++)
                {
                    Console.WriteLine(file[i]);
                }
            }
            else
            {
                Console.WriteLine("文件夹不存在");
            }
            Console.Read();
        }
    }

 

用foreach循环输出
class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\新建文件夹";
            if (Directory.Exists(path))
            {
                string[] dire = Directory.GetFiles(path);
                string[] file = Directory.GetDirectories(path);
                foreach (string item in dire)
                {
                    Console.WriteLine(item);
                }
                foreach (string str in file)
                {
                    Console.WriteLine(str);
                }
            }
            else
            {
                Console.WriteLine("文件夹不存在");
            }
            Console.Read();
        }
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
相关资源
相似解决方案