Map network drive: "net.exe USE" vs WshNetwork.MapNetworkDrive?
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/
Related videos on Youtube
Comments
-
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 about 12 yearsI like #2. It looks cleaner, and was provided as part of an API. However, what about being able to specify credentials, etc?
-
Jared DC about 12 yearsYes you can use credentials. I just left that out for clarity.
-
user1703401 about 12 years
-
Kiquenet almost 11 yearsBetter practices for net use: stackoverflow.com/questions/8919/…
-
Kiquenet almost 11 yearsany final solution with full source code sample working about it ?
-
Jared DC almost 11 yearsI was approved to use the class mentioned in the accepted answer below. It works great.
-
-
Jared DC about 12 yearsThat would be perfect if it didn't use a 3rd party created class. I'm prevented from using them.
-
Jim Moriarty about 12 yearsYou don't need to use his class. You can create your own based on his work.