What is the difference between File and FileInfo in C#?

后端 未结 10 1684
我在风中等你
我在风中等你 2020-12-02 11:22

I\'ve been reading that the static methods of the File Class are better used to perform small and few tasks on a file like checking to see if it exists and that

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 11:28

    The File.Exists will perform much faster than a new FileInfo(filePath).Exists - especially over a network and provided the files actually exist. This is because File.Exists will only check for existence of the file, whereas a new FileInfo(filePath).Exists first constructs a FileInfo object, which contains all the properties (dates, size etc) of the file (if it exists).

    In my experience with this, even checking for the existence of 10 files over the network is noticeably faster (ie 20ms vs 200ms) by using File.Exists.

提交回复
热议问题