Programmatically Mapping Network Drives on Windows 10

1.1k Views Asked by At

I maintain a drive mapping utility where I work. It's only real purpose is to map network drives. It's been around for a while and has been updated through the years, as operating systems are upgraded. The code for mapping the drives is:

    Process prMapDrive;
    prMapDrive = new Process();

    prMapDrive.StartInfo.UseShellExecute = true;
    prMapDrive.StartInfo.FileName = "cmd.exe";
    prMapDrive.StartInfo.Arguments = string.Format(@"/C net use {0} {1} /persistent:{2}", sDrive, sPath, fPersist ? "Yes" : "No");
    prMapDrive.StartInfo.CreateNoWindow = true;
    prMapDrive.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    prMapDrive.Start();
    prMapDrive.WaitForExit(); 
    prMapDrive.Close();

    prMapDrive.StartInfo.Verb = "runas";
    prMapDrive.Start();
    prMapDrive.WaitForExit();
    prMapDrive.Close();

This code works great on Windows 7 machines and I have no complaints. The complaints come in when people use it on Windows 10. When run on Windows 10, the drives are not visible. I added the following line of code, to check for the presence of the drive:

    return Directory.Exists(sDrive);

This returns True, yet when I go into explorer, I don't see the drive. If I go to a dos prompt and check for the drive using net use, I don't see it. So in spite of Directory.Exists returning True, I don't see any mapped drives. This does not appear to be failing and I suppose that on some level, it isn't. Otherwise Directory.Exists would not be returning True.

To make matters worse, this is a newer version of an older VB.NET utility. That utility works, at least for me, though there are some complaining that it doesn't work. The VB.NET code for it is:

  Dim prMapDrive As Process
  prMapDrive.StartInfo.UseShellExecute = True
  prMapDrive.StartInfo.FileName = "cmd.exe"
  prMapDrive.StartInfo.Arguments = String.Format("/C net use {0} {1} /persistent:{2}", sDrive, sPath, _
                                                 If(fPersist, "Yes", "No"))

  prMapDrive.StartInfo.CreateNoWindow = True
  prMapDrive.StartInfo.WindowStyle = ProcessWindowStyle.Hidden

  prMapDrive.Start()
  prMapDrive.WaitForExit()
  prMapDrive.Close()

  prMapDrive.StartInfo.Verb = "runas"

  prMapDrive.Start()
  prMapDrive.WaitForExit()
  prMapDrive.Close()

  Return Directory.Exists(sDrive)

I don't see any difference between these two, other than the fact that they're using different programming languages.

Looking in the registry in Windows 7, I see drives mapped under HKCU\Network. When I look on the Windows 10 machine, I see no drives mapped. So that's one difference.

Another difference - if I use an elevated command prompt, then I can see the drives using net use, and I can access them through DOS.

So it does appear that the drives are being mapped and can be accessed from an elevated command prompt.

What am I missing? Is there some other way that I should be handling this? I used to use WNetAddConnection, but stopped using it on Windows 10 because that didn't seem to be working.

0

There are 0 best solutions below