简体   繁体   中英

Can't access mapped network drive

I have a ConsoleApp (not asp.net) that takes files from directories (The app runs on Windows Server 2012). The app works fine with local dirs and even shared dirs (ex. "\\\\MyShare\\dest"). However, when i map the share (from "\\\\MyShare to X:) i get

DirectoryNotFoundException.

I have to map the drive because some of the files are exceeding the 260 letters limit. Furthermore, when i debug my app on my pc, i don't get an error while accessing the mapped drive.

Thanks in advance for any help :)

PS: i've seen other posts that the problem is the app doesn't run as the right user privileges. My app runs with my credentials so the map exists for my user..

EDIT: i did a little workaround with your help and it worked. Instead of creating a mapped network drive i used the mklink command and it made a shortcut for my share:

mklink /D c:\\MyShortcut \\\\MyShare

Thanks for the help everyone

If I understood the question and comments correctly, the mapped drive cannot be accessed as the mapping is not visible to the application. To my understanding it is possible to programmatically connect to the share using platform invoke, more precisely the following two functions and structure.

[DllImport("Mpr.dll")] static extern int WNetUseConnection(
    IntPtr hwndOwner,
    NETRESOURCE lpNetResource,
    string lpPassword,
    string lpUserID,
    int dwFlags,
    string lpAccessName,
    string lpBufferSize,
    string lpResult
);

[DllImport("Mpr.dll")] static extern int WNetCancelConnection2(
    string lpName,
    int dwFlags,
    bool fForce
);

[StructLayout(LayoutKind.Sequential)] class NETRESOURCE
{ 
    public int dwScope = 0;
    public int dwType = 0;
    public int dwDisplayType = 0;
    public int dwUsage = 0;
    public string lpLocalName = "";
    public string lpRemoteName = "";
    public string lpComment = "";
    public string lpProvider = "";
}

The functions are documented here and here ; this and this question are possibly related.

One possibility is that your application is running in elevated mode (ie. with Run as administrator selected). The problem is discussed in greater detail here .

The solution is to use net use to mount the drive, mklink as you have done or simply merge this into your registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"EnableLinkedConnections"=dword:00000001

A reboot is required after making the above change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM