36

I've a problem with the way File.Exists() (doesn't) work: when I use it, it claims that the file doesn't exist (from Immediate Window):

filePath
"P:\\poolman\\LY21\\2015\\LY21_2015-03-25_03.xml"
File.Exists(filePath)
false

But if I copy/paste the file path to an explorer window url (removing the escaping \) it opens the file.

So File.Exists() claims that an existing file doesn't exist which bug me.

It's not about the length of the path (which is 43) and FileInfo is not a better option as suggested here.

Here's the result of the FileInfo check:

var f = new FileInfo(filePath);
{P:\poolman\LY21\2015\LY21_2015-03-25_03.xml}
    base: {P:\poolman\LY21\2015\LY21_2015-03-25_03.xml}
    _name: "LY21_2015-03-25_03.xml"
    Directory: {P:\poolman\LY21\2015}
    DirectoryName: "P:\\poolman\\LY21\\2015"
    Exists: false
    IsReadOnly: true
    Length: '(var f = new FileInfo(filePath);).Length' threw an exception of type 'System.IO.FileNotFoundException'
    Name: "LY21_2015-03-25_03.xml"

How could I deal with it?

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • 4
    Do some research, check the documentation: _`true` **if the caller has the required permissions** and path contains the name of an existing file_. What are the file's permission and as which user is your process running? I guess you're running this in an ASP.NET context which doesn't have the appropriate permissions. – CodeCaster Sep 22 '15 at 10:57
  • @PaulZahra this is a mapped drive – Thomas Ayoub Sep 22 '15 at 10:59
  • @CodeCaster VS is running in admin mode but this may not answer your question... And to me this doesn't explain the FileInfo.Exists being false – Thomas Ayoub Sep 22 '15 at 10:59
  • Is this a Windows app or a web app? – DavidG Sep 22 '15 at 11:01
  • 2
    This should not be marked a duplicate of the suggested query since the suggested query dows not take into account the fact that VS is running with elevated permissions which is the real problem, the suggested duplicate does not discuss this point which this does and for me, this questions selected answer DO answer my question, the suggested dup does not answer my question. – David Mårtensson Dec 01 '17 at 10:23
  • 1
    As a mere mortal, my VS is not seeing mapped drive when run as admin. I'm definitely not going to search "Why does System.IO.File.Exists(string path) return false". – Zach Smith Jul 20 '18 at 11:08
  • 1
    @ZachSmith that's why I didn't delete the question and it got that number of upvotes ;) – Thomas Ayoub Jul 20 '18 at 12:04
  • It's ridiculous that this was closed. For me, the [EnableLinkedConnections](https://learn.microsoft.com/en-us/troubleshoot/windows-client/networking/mapped-drives-not-available-from-elevated-command) solution works fine for network shares on Windows 10. For local folder mappings to a drive just do subst in an elevated terminal. – Nick Westgate Aug 31 '22 at 12:11

1 Answers1

45

If you run a process (such as Visual Studio) elevated (as you claim in comments), it's not running as your current Windows user, but as Administrator.

Administrator does not have the drive mappings that your user has. So your Visual Studio cannot see the P: drive at all, because that mapping is specific to your user.

See How to access network shares from an elevated process in Windows 7?: if this error is caused by your current user having the P: drive mapped to a network drive, you can use the UNC path to the share instead: \\server\share\file.xml, where P: would be mapped to \\server\share\.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • This seems to be the right answer. How can I map this drive to admin ? – Thomas Ayoub Sep 22 '15 at 11:04
  • 5
    Don't map it, use the full UNC path instead, e.g. `\\server\folder\path\file.txt` – DavidG Sep 22 '15 at 11:04
  • 2
    @DavidG It's not that easy because I'm dealing with a few stuff that I won't change... But now that I know why, I can continue in peace – Thomas Ayoub Sep 22 '15 at 11:07
  • 1
    @Thomas you can for example put the base path in configuration, like the AppSettings section. Then in debug it's an UNC path ("\\server\folder\Xml\") and in release it's the mapped drive ("P:\Xml\"). Of course then chances are Administrator can't access the share, so then you'll have to let the user login to the share as Paul demonstrates. – CodeCaster Sep 22 '15 at 11:08
  • had the same issue on a build machine - visual studio couldn't find some files on a network drive – user2989573 Jan 31 '19 at 20:44
  • Yep, nice little feature. Makes perfect sense and you are 100$ correct. – S. J. Mar 06 '19 at 16:13
  • 2
    In case you don't know what the UNC paths are for your mapped drives, run `net use` on cmd as your user. [Net use docs](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/gg651155(v%3Dws.11)) – nyghtly Dec 27 '19 at 17:08