• 整理文件操作(三)File.Exists(path)和new FileInfo(path).Exists


      在微软提供的介绍中,FileFileInfo有完整的介绍。

      我目前只整理,自己用到的方法。比如判断文件是否存在。  

     static void Main(string[] args)
            {
                string path = @"E:	estfile01.txt";
    
                bool flag = File.Exists(path);
    
                if (flag)
                {
                    Console.WriteLine("文件存在");
                }
                else
                {
                    Console.WriteLine("文件不存在");
                }
    
                Console.Read();
            }

      在微软的文档网页上,介绍的很详细。还说Exist方法不应使用的路径验证时,若要检查目录是否存在,使用Directory.Exists。File.Exists只是检查在指定的文件是否存在。

      下面是用FileInfo类来,完成检查指定文件是否存在。

     static void Main(string[] args)
            {
    
                string path = @"E:	estfile01.txt";
    
                FileInfo fInfo = new FileInfo(path);
    
                bool flag = fInfo.Exists;
    
                if (flag)
                {
                    Console.WriteLine("文件存在");
                }
                else
                {
                    Console.WriteLine("文件不存在");
                }
    
                Console.Read();
            }

      这里是微软的介绍,FileInfo是需要实例一个有string类型的构造函数,而且Exists是对象的一个属性。

      那么,我此时有这样的问题。为何有File静态类和FileInfo实例类,两种呢,它们都有具备操作文件的功能。

      现在,不纠结这个问题。使用.net framework提供的两个类,来检查文件是否存在,我会了。

  • 相关阅读:
    I.MX6 Surfaceflinger 机制
    理解 Android Fragment
    RPi 2B DDNS 动态域名
    RPi 2B IPC webcam server
    理解 Android MVP 开发模式
    I.MX6 system.img unpack repack
    can't set android permissions
    VMware Ubuntu 共享文件夹
    解决oracle数据库连接不上的问题
    perfect-scrollbar示例
  • 原文地址:https://www.cnblogs.com/158-186/p/10939532.html
Copyright © 2020-2023  润新知