How can I access a mapped network drive with System.IO.DirectoryInfo?
Asked Answered
P

7

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.

Payson answered 25/9, 2008 at 14:24 Comment(1)
Is z: definitely being mapped to a regular share or an administrative share e.g. C$, D$ etcDeformed
D
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.

Deformed answered 25/9, 2008 at 14:39 Comment(2)
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!Bases
@AndreasJansson - glad I could :)Deformed
M
18

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"

Mahout answered 15/4, 2014 at 14:53 Comment(3)
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.Rhodesia
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."Turro
@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.Mahout
S
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.

Sophia answered 25/9, 2008 at 14:40 Comment(1)
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 \\\\server1Ophthalmitis
W
3

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

Whyalla answered 25/9, 2008 at 14:31 Comment(0)
R
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.

Regimentals answered 26/9, 2008 at 8:4 Comment(0)
M
1

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

Muhammad answered 25/9, 2008 at 14:30 Comment(0)
R
0

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

Rosenberger answered 10/6, 2016 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.