40

I need to create a directory on a mapped network drive. I am using a code:

DirectoryInfo targetDirectory = new DirectoryInfo(path);
if (targetDirectory != null)
{
    targetDirectory.Create();
}

If I specify the path like "\\\\ServerName\\Directory", it all goes OK. If I map the "\\ServerName\Directory" as, say drive Z:, and specify the path like "Z:\\", it fails.

After the creating the targetDirectory object, VS shows (in the debug mode) that targetDirectory.Exists = false, and trying to do targetDirectory.Create() throws an exception:

System.IO.DirectoryNotFoundException: "Could not find a part of the path 'Z:\'."

However, the same code works well with local directories, e.g. C:.

The application is a Windows service (WinXP Pro, SP2, .NET 2) running under the same account as the user that mapped the drive. Qwinsta replies that the user's session is the session 0, so it is the same session as the service's.

Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • 1
    Is z: definitely being mapped to a regular share or an administrative share e.g. C$, D$ etc – Kev Sep 25 '08 at 14:36

7 Answers7

61

Mapped network drives are user specific, so if the app is running under a different identity than the user that created the mapped drive letter (z:) it won't work.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 1
    It is the answer for me too. I suddenly had errors in Visual Studio, that I had not seen before, and people with the same error wrote that you had to run VS as Administrator (which I don't think I've ever done before, being part of the administrator group on my own computer already). This is the day after, and I had forgotten about it. Getting this path errors for a file that I could copy the day before... Thank you Kev! – Andreas Jansson Nov 19 '18 at 16:14
  • @AndreasJansson - glad I could :) – Kev Nov 20 '18 at 17:47
17

Based on the fact, mapped drive letters don't work, the simple solution is to type the full network path.

Aka,

my R:/ drive was mapped to \\myserver\files\myapp\

So instead of using

"R:/" + "photos"

use

"\\myserver\files\myapp\" + "photos"

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • 2
    One issue with using the full network path is that it increases the length of the file path such that "R:\...\FileA.txt" may work for copying, deleting, etc. but "\\myserver\files\myapp\...\FileA.txt" may fail due to the path being too long. – skeletank Oct 13 '15 at 12:26
  • This did not work for me. I am trying to map an Azure file share. Using the drive letter I get: "Could not find a part of the path Q:\" and if I use the UNC path, I get: "The user name or password is incorrect." – Dan Csharpster Jun 14 '17 at 21:19
  • @DanCsharpster It sounds to me like it IS working. But you are providing an invalid username or password... Othwerwise you would be getting path not found still. – IAmGroot Jun 15 '17 at 08:30
5

The account your application is running under probably does not have access to the mapped drive. If this is a web application, that would definitely be the problem...By default a web app runs under the NETWORK SERVICE account which would not have any mapped drives setup. Try using impersonation to see if it fixes the problem. Although you probably need to figure out a better solution then just using impersonation. If it were me, I'd stick to using the UNC path.

Erikk Ross
  • 2,173
  • 13
  • 18
  • 4
    UNC path was the quick fix that worked for me. For those that don't know, that's a path like this: \\server1\folder1\folder2\file.pdf - remember that for a c# string you'll need to escape your backslashes like this \\\\server1 – Matt Kemp Apr 05 '13 at 01:35
3

Are you mapping with the exact same credentials as the program is running with?

EBGreen
  • 36,735
  • 12
  • 65
  • 85
2

Are you running on Vista/Server 2k8? Both of those isolate services into Session 0 and the first interactive session is Session 1. There's more info here, on session isolation. Thus, even if it's the same user being used for both the service and the interactive logon, it'll be different sessions.

MrLore
  • 3,759
  • 2
  • 28
  • 36
Rob
  • 45,296
  • 24
  • 122
  • 150
1

You can try to use WNetConnection to resolve the mapped drive to a network path.

fhe
  • 6,099
  • 1
  • 41
  • 44
0

I had the same problem on Win Server 2012. The disabling UAC solved it.