Determining if file exists using c# and resolving UNC path

后端 未结 6 637
星月不相逢
星月不相逢 2020-11-27 08:17

I am trying to write a function to determine if a file exists. The two methods prove to return inconsistent results (fileExists() seems to provide accurate results, compared

6条回答
  •  一整个雨季
    2020-11-27 08:53

    You can create a FileInfo for an non-existing file. But then you can check the FileInfo.Exists property to determine whether the file exists, e.g:

    FileInfo fi = new FileInfo(somePath);
    bool exists = fi.Exists;
    

    Update: In a short test this also worked for UNC paths, e.g. like this:

    FileInfo fi = new FileInfo(@"\\server\share\file.txt");
    bool exists = fi.Exists;
    

    Are you sure that the account (under which your application is running) has access to the share. I think that (by default) administrative rights are required to access the share "c$".

提交回复
热议问题