Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Directory.Exists denies existence of mapped network drive when running as admin

I'm writing a small .NET program on Windows 7. One thing it needs to do is to create symbolic links, which seems to require me to have administrator privileges. It also needs to be able to work with mapped network drives (for example, R:\, which on my system maps to \\titanium\Private\).

I'm using Directory.Exists(path) to verify that a path exists.

When running the program as a regular user (administrator account, but not "as administrator"), this works fine on the mapped network drive.

When running the program as an administrator (with UAC), it fails to find directories that exist. As a result, the program refuses to acknowledge that R:\Steam Games\ is a directory that actually exists.

I'm a bit baffled as to why this is happening. Using the full UNC path (\\titanium\Private\Steam Games\) also doesn't work.

Has anyone run into this before? Is there any good workaround? Do I have to format the paths different (note: most of them are currently formatted with Path.Combine, so they should be correct).

Thanks for your help.

(As an example, Directory.Exists(@"R:\Steam Games\") returns false when running as an admin, but that folder exists. The function call correctly returns true when running regularly).

Edit: The issue indeed appears to be that an administrator is technically a different user account. I could not even use UNC paths, because I was only logged in to my fileserver under my regular user, not under "Administrator". As a (relatively hackish) workaround, I just run my program with regular privileges and then use Process.Start to invoke an instance of cmd.exe with the arguments to create a symbolic link (and verb "runas" to get the UAC prompt).

like image 394
Ethan Avatar asked Jul 23 '11 21:07

Ethan


People also ask

How do you remove mapped drive the network connection does not exist?

When you right click to disconnect a drive mapping, you get 'This network connection does not exist'. To fix this issue, open a command prompt and type 'regedit' to access the registry. From the registry editor, select Edit->Find. In the search field, enter 'MountPoints2' and search for registry keys.


1 Answers

Mapped drives in windows are tied to a user context. A drive mapped to a account: UserA, will not be accessible to UserB. You could create the same unc path mapped to the same drive letter under different users though.

Using the full unc path should work though.

More info at support.microsoft.com: 1, 2.

like image 125
mservidio Avatar answered Oct 26 '22 08:10

mservidio