[C#学习笔记]判断文件文件夹是否存在

判断文件或者文件夹是否存在

   首先,文件File,文件夹Directory,需要引用System.IO

using System.IO;
    定义一个需要判断的文件的地址

string filePath = "C:\\test.txt" ;
    然后利用File进行判断

            if (File.Exists(filePath))
            {
                Console.WriteLine("文件{0}存在.", filePath);
            }
            else
            {
                Console.WriteLine("文件{0}不存在.", filePath);
            }

   运行结果如下图所示

[C#学习笔记]判断文件文件夹是否存在_第1张图片

   定义一个文件夹的地址

string folderPath ="C:\\Windows";

    利用Directory进行判定

            string folderPath = "C:\\Windows";
            if (Directory .Exists (folderPath ))
            {
                Console.WriteLine(" 文件夹{0}存在.", folderPath);
            }
            else 
            {
                Console.WriteLine("文件夹{0}不存在.", folderPath);
            }
            Console.ReadKey();
    运行结果如下图

[C#学习笔记]判断文件文件夹是否存在_第2张图片








你可能感兴趣的:(C#)