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

后端 未结 6 1391
萌比男神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 22:53

    I just found this thread and wanted to update it as I had an issue with FileInfo vs File.Exists.

    Let's take a scenario where we create a fileinfo object for a file that currently doesn't exist on a UNC share.

    bool fileInfo  = new FileInfo(@"\\uncshare\name\filename.txt");
    

    At this point, the file does not exist, but if I create it using another tool (other code or outside my app) and then do this...

    fileInfo.Refresh();
    bool exists = fileInfo.Exists;
    

    The result is false, it does not exist, even though it does and the fileInfo was refreshed.

    To get the correct result requires..

    bool exists = File.Exists(f);
    

    Hope that helps others.

提交回复
热议问题