0

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 for 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 tried the code in windows and exist is true. I don't know different things between Mac and Windows.

I think that the issue is that FileInfo does not actually access the shared drive and path in the argument, checking the fileinfo with FullPath properties as below.

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

It is under User path. Please tell me why and how to solve.

  • _I waste a lot of time to findout_: If it's something that you need to accomplish, and you don't know how to do it, the time isn't _wasted_. You've invested time to learn something new. How long should it take? There's no specific amount of time. It takes as long as it takes. – Tu deschizi eu inchid Apr 26 '23 at 02:47
  • Does this answer your question? [How should I handle windows/Linux paths in c#](https://stackoverflow.com/questions/5758633/how-should-i-handle-windows-linux-paths-in-c-sharp) – KennetsuR Apr 26 '23 at 02:47
  • Try searching for [macos unc path](https://www.google.com/search?q=macos+unc+path) – Tu deschizi eu inchid Apr 26 '23 at 02:49
  • @KennetsuRinn I don't think so. Because the full path is under User path: "/Users/me/Projects/CurrentProjectFolder/\\\\servername\\folder\\file.pdf". So I think it is root cause. But I don't know how to fix – dmvn denso Apr 26 '23 at 02:56

1 Answers1

0

The share from Windows will be mounted under /Volumes on macOS. If you mounted folder from servername, the local path will be /Volumes/folder.

e.g.

@"/Volumes/folder/file.pdf"

(If you open the Terminal app and drag the icon of the mounted volume onto the terminal window, the path of the mounted volume will be added at the current cursor position.)

\\servername\folder\file.pdf is a UNC (Universal Naming Convention) path and it appears that UNC paths are only supported on Windows.

The UNC path is not recognized and is being interpreted as not rooted. Because it is not rooted the current directory is prepended resulting in:

/Users/me/Projects/CurrentProjectFolder/\\\\servername\\folder\\file.pdf
Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14