UNC share directory does not exist

2.5k Views Asked by At

I have a C# process that needs to read a file existing in a share directory on a remote server.

The below code results in "Share does not exist." being written to the console.

string fileName = "someFile.ars";
string fileLocation = @"\\computerName\share\";
if (Directory.Exists(fileLocation))
{
    Console.WriteLine("Share exists.");
}
else
{
    Console.WriteLine("Share does not exist.");
}

The process is run under an AD user account and the same account is granted Full Control permissions on the share directory. I can successfully map the share as a network drive on the machine the process resides on and can copy files to/from the directory. Any ideas on what I'm missing?

1

There are 1 best solutions below

1
On

Use File.Exists rather than Directory.Exists.

Plus, you may want to be somewhat platform-agnostic and use the canonical Path.Combine as such:

string fileName = "someFile.ars";
string fileServer = @"\\computerName";
string fileLocation = @"share";
if (File.Exists(Path.Combine(fileServer, fileLocation, fileName)))
{
    Console.WriteLine("Share exists.");
}
else
{
    Console.WriteLine("Share does not exist.");
}