zoukankan      html  css  js  c++  java
  • 整理文件操作(三)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提供的两个类,来检查文件是否存在,我会了。

  • 相关阅读:
    PostgreSQL数据库笔记
    LayUI
    Spring
    Mybatis
    Mybatis生成
    server服务器信息页面添加步骤
    Java数据类型和MySql数据类型对应表
    空字符串
    json解析尖括号<>
    JSON--List集合转换成JSON对象
  • 原文地址:https://www.cnblogs.com/158-186/p/10939532.html
Copyright © 2011-2022 走看看