Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
like image 683
JimDel Avatar asked Mar 20 '12 01:03

JimDel


1 Answers

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/

like image 125
dvallejo Avatar answered Oct 19 '22 15:10

dvallejo