0

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

P.S: 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

  • Are you running your app as a scheduled task? – MatSnow Aug 23 '17 at 14:36
  • is UAC in use? do you run unattended, or from VS? how do you access and use the files and directories (include code). otherwise this should go to serverfault.stackexchange.com – Cee McSharpface Aug 23 '17 at 14:36
  • @MatSnow windows service, the app always runs – sol6michael6 Aug 24 '17 at 06:47
  • @user8506804 A windows service is running its own session. Because of this it has no access to your mapped drive. For further information read [this](https://msdn.microsoft.com/en-us/library/windows/desktop/ms685143%28v=vs.85%29.aspx). – MatSnow Aug 24 '17 at 07:05

2 Answers2

1

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.

AlainD
  • 5,413
  • 6
  • 45
  • 99
0

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.

Codor
  • 17,447
  • 9
  • 29
  • 56