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

后端 未结 10 1657
我在风中等你
我在风中等你 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条回答
  • 2020-12-02 11:37

    FileInfo is an instance of a file thus representing the file itself. File is a utility class so can work with any file

    0 讨论(0)
  • 2020-12-02 11:41

    File is optimized for one-off operations on a file, FileInfo is optimized around multiple operations on the same file, but in general there isn't that much difference between the different method implementations.

    If you want to compare the exact implementations, Use Reflector to look at both classes.

    0 讨论(0)
  • 2020-12-02 11:45

    FileInfo:

    • Need to instantiate before using
    • Contains instance methods
    • Cache Info about the File and you need to call Refresh every time to get the latest info about the File

    File:

    • No need to instantiate
    • Contains static methods
    • Do not cache, so you get latest info every time you use it.

    src:

    • FileInfo
    • File
    0 讨论(0)
  • 2020-12-02 11:54

    Recently I faced problem with File.Exist, I hate this function. After than I've used Fileinfo class Exist function then my program works correct.

    Actually what happen in development enviornment File.Exist works well but when it goes to live environment this function is blocking the file object due to that reason I am getting the error access denied and not able to use the file.

    This is my learning. I will never used File.Exist method best is to create the object and then use it. Be aware to use static methods.

    0 讨论(0)
提交回复
热议问题