tags:

views:

2330

answers:

6

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.

A: 

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

fhe
A: 

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

EBGreen
+2  A: 

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
+1  A: 

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
A: 

Thank you for all the answers, but I am afraid this does not give me a clue. 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.

A: 

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.

Rob