问题描述
我想在我的程序中映射一个驱动器.我的用户可以使用从 XP 开始的每个版本的 Windows.所以我需要最通用的方法.我以前使用过第一种方法,通常对我来说效果很好.但是真的没有办法用它来捕捉错误(我知道无论如何).第二个很容易让我在它周围包裹一个 try/catch 块,但是对于我为方法 1 的替代方法所做的所有搜索,我只遇到了方法 2 一次.所以这让我想知道它对于如此多变的环境是否足够可靠.谁能告诉我方法 2 在大多数情况下是否安全?
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?
方法一
Process.Start("net.exe", @"USE Z: \\server\share /user:domain\username password").WaitForExit();
方法 2 引用 Windows 脚本宿主对象模型
Method 2 referencing the Windows Script Host Object Model
IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("k:", @"\\server\share");
推荐答案
另一种方法是调用实际的 Win32 api(WNetAddConnection2A、WNetCancelConnection2A 等).查看 http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c12357/
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/
这篇关于映射网络驱动器:“net.exe USE"VS WshNetwork.MapNetworkDrive?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!