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

后端 未结 6 1392
萌比男神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:36

    in your first case, the file path is incorrect, you need spaces in "Program Files (x86)".

    Secondly, the Path.Combine will return a Directory path so you'll end up with something like "C:\Program Files (x86)\MyAppFolder\Manager.exe\" so it's a bad idea.

    Both methods work the same way, so make sure you check that the path is correct.

    0 讨论(0)
  • 2021-02-13 22:52

    I've replicated your scenario using the below Linqpad script

    var f = @"C:\Program Files (x86)\MyAppFolder\manager.exe";
    
    bool fileExists = File.Exists(f);
    bool fileInfoExists = new FileInfo(f).Exists;
    
    fileExists.Dump();
    fileInfoExists.Dump();
    

    Ran this both when the file existed and when it did not and both produced the same output each time. Maybe try this on your system and see if you still see differences.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • The difference between File.Exists() and new FileInfo().Exists on it's behavior when full path (directory name + file name) is long:

    var f = @"C:\Program Files (x86)\MyAppFolder\many_subfolders\manager.exe";
    
    //f.length > 260 characters
    
    bool fileExists = File.Exists(f); //return false, even if the file exists
    
    // Throw exception: "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."
    bool fileInfoExists = new FileInfo(f).Exists;
    
    0 讨论(0)
  • 2021-02-13 23:00

    There is no difference, these methods use the exact same internal helper method inside the .NET Framework. Something you can see with a decompiler or the Reference Source source code, the helper method name is File.FillAttributeInfo().

    Having duplication like this in the .NET Framework is pretty unusual, not exactly a Good Thing to have more than one way to accomplish the same thing. The File class is however special, it got added after a usability study conducted when .NET 1.0 shipped. The test subjects just had the basic BCL classes to work with, like FileStream and FileInfo, and otherwise only had MSDN documentation available. The test results were not very good, the File class got added to help programmers fall in the pit of success writing very basic file manipulation code. Like File.Exists() and File.ReadAllLines().

    So it doesn't have anything to do with the classes, you are just using them wrong. Like not actually using the same path. Do go easy on the forward slashes, the mapping to backward slashes happens inside Windows and is inconsistently implemented in other code. Using // certainly doesn't do what you hope it does.

    0 讨论(0)
  • 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.

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