3

I am trying to find out if a file exists in a shared drive from a Windows machine in the local network. The drive can be easily mounted and accessed with my machine and it does not require credentials. I am using Visual Studio on Mac

I am using the following code in c# and exists is false:

FileInfo fileInfo = new FileInfo(@"\\servername\folder\file.pdf");
bool exists = fileInfo.Exists;

I think that the issue is that FileInfo does not actually access the shared drive and path in the argument, but checking for the following instead:

"/Users/me/Projects/CurrentProjectFolder/\\\\servername\\folder\\file.pdf"

The above path is the value of the FullPath and FullName property of the fileInfo object and I believe it does not find the file because it actually checks for this full path.

For example, trying to get the files for that directory throws a DirectoryNotFoundException.

Directory.GetFiles(@"\\servername\folder\");

Could not find a part of the path '/Users/me/Projects/CurrentProjectFolder/\\servername\folder\'.

I have been spending quite some time for a solution and there a high number of answers that follow the exact same operation I have pasted above - FileInfo with a UNC patch as argument.

Thanks

msaitz
  • 35
  • 2
  • 7
  • What error are you getting? What's causing the error? – Jaskier Sep 28 '18 at 15:04
  • 1
    Possible duplicate of [Determining if file exists using c# and resolving UNC path](https://stackoverflow.com/questions/458363/determining-if-file-exists-using-c-sharp-and-resolving-unc-path) – Eugene Sep 28 '18 at 15:05
  • @GrantWinney , then my thought would be just a simple `if` statement would do the trick for checking if the file exists. Correct me if I'm wrong :) – Jaskier Sep 28 '18 at 15:08
  • Have you verified the file path and made sure that you can browse to it on your own? (sorry for the dumb question) – Jaskier Sep 28 '18 at 15:22
  • @Symon yes, the file is there and can be accessed. I have updated the description to show that I'm getting an Exception when using Directory.GetFiles() – msaitz Sep 28 '18 at 15:34
  • 2
    Hi @msaitz A bit of a random stab in the dark but I wonder if this is related to the direction of the slashes and the differences between running on windows and on MAC. If you change the path you are checking to this '//servername/folder/file.pdf' and see if that helps – Dave Sep 28 '18 at 15:38
  • @Dave trying it has brought something interesting that I don't know yet if it can point to a solution, but changing the direction of the two first slashes makes the full path as "Could not find a part of the path '/servername/folder/'" as if it was located in the root directory. – msaitz Sep 28 '18 at 15:48
  • 1
    UNC is such a misnomer I've found, I've definitely had cross platform issues. I'm at work atm, when I'm home if you still don't have an answer I'll see if I can dig up some info from deep in my brain – Dave Sep 28 '18 at 15:52
  • Note about the [tag:visual-studio] tag: if you read the description, it says *"Use this tag if you have a specific question about Visual Studio features and functionality. DO NOT use this tag on questions regarding code which merely happened to be written in Visual Studio."* – Richardissimo Sep 29 '18 at 07:17

0 Answers0