C# dotnet 使用判断文件夹存在的方法判断一个文件路径会怎样

时间:2022-07-22
本文章向大家介绍C# dotnet 使用判断文件夹存在的方法判断一个文件路径会怎样,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

假定我有一个文件的路径,我将这个文件路径放在文件夹判断方法里面,请问此时返回的是存在还是不存在?答案是返回不存在

如下面测试代码,这里的 GelteajoutrerebaKoutigasremawcho.dll 就是程序集,也就是文件是存在的,那么放在 DirectoryInfo 判断输出的内容是否存在

namespace GelteajoutrerebaKoutigasremawcho
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo dir = new DirectoryInfo(@"GelteajoutrerebaKoutigasremawcho.dll");
            Console.WriteLine(dir.Exists);
            
            var file = new FileInfo(@"GelteajoutrerebaKoutigasremawcho.dll");
            Console.WriteLine(file.Exists);
        }
    }
}

输出是 False True 也就是判断文件夹不存在,判断文件存在

代码放在 github 欢迎小伙伴访问