Map network drive: "net.exe USE" vs WshNetwork.MapNetworkDrive?

15,333

The other approach would be to pinvoke the actual Win32 apis (WNetAddConnection2A, WNetCancelConnection2A, etc). Check out http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c12357/

Share:
15,333

Related videos on Youtube

Jared DC
Author by

Jared DC

Just Still learning C#

Updated on June 04, 2022

Comments

  • Jared DC
    Jared DC almost 2 years

    I'm looking to map a drive in my program. My users can be using every version of Windows from XP on up. So I need the most versatile method. I have used the first method before and generally it's worked well for me. But there really isn't way to catch errors with it (that I know of anyway). The second will easily let me wrap a try/catch block around it, but for all the searching I've done for an alternatives to method 1, I've only run across method 2 once. So that leaves me to wonder if its reliable enough for such a varied environment. Can anyone tell me if method 2 is safe for most circumstances?

    Method 1

    Process.Start("net.exe", @"USE Z: \\server\share /user:domain\username password").WaitForExit();
    

    Method 2 referencing the Windows Script Host Object Model

    IWshNetwork_Class network = new IWshNetwork_Class(); 
    network.MapNetworkDrive("k:", @"\\server\share");
    
    • Admin
      Admin about 12 years
      I like #2. It looks cleaner, and was provided as part of an API. However, what about being able to specify credentials, etc?
    • Jared DC
      Jared DC about 12 years
      Yes you can use credentials. I just left that out for clarity.
    • user1703401
      user1703401 about 12 years
    • Kiquenet
      Kiquenet almost 11 years
      Better practices for net use: stackoverflow.com/questions/8919/…
    • Kiquenet
      Kiquenet almost 11 years
      any final solution with full source code sample working about it ?
    • Jared DC
      Jared DC almost 11 years
      I was approved to use the class mentioned in the accepted answer below. It works great.
  • Jared DC
    Jared DC about 12 years
    That would be perfect if it didn't use a 3rd party created class. I'm prevented from using them.
  • Jim Moriarty
    Jim Moriarty about 12 years
    You don't need to use his class. You can create your own based on his work.