What is difference between File.Exists(“”) and FileInfo exists

后端 未结 6 1378
萌比男神i
萌比男神i 2021-02-13 22:27

I have an *.exe file in \\ProgramFiles(x86)\\MyAppFolder.

In x86 application I check if the file exists (64 bit system). simple:

bool fileExists = File.E         


        
6条回答
  •  我在风中等你
    2021-02-13 23:03

    This is about the only difference and it has more to do with the nature of FileInfo:

    FileInfo fileInfo = new FileInfo("myFile.txt"); // non-existent file
    Console.WriteLine(fileInfo.Exists);             // false
    File.Create("myFile.txt");
    Console.WriteLine(File.Exists("myFile.txt"));   // true
    Console.WriteLine(fileInfo.Exists);             // false
    

    So as you can see the value of fileInfo.Exists is cached the first time you use it.

    Other than that, they do the same thing behind the scenes.

提交回复
热议问题