Issue when checking if file exists in shared drive in macOS

125 views Asked by At

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.

1

There are 1 answers

1
Jonathan Dodds On BEST ANSWER

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