我在我工作的地方维护一个驱动器映射实用程序。唯一真正的目的是映射网络驱动器。随着操作系统的升级,它已经存在了一段时间并且多年来一直在更新。映射驱动器的代码是:
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();
此代码在 Windows 7 机器上运行良好,我没有任何抱怨。当人们在 Windows 10 上使用它时,抱怨就会出现。在 Windows 10 上运行时,驱动器是不可见的。我添加了以下代码行,以检查驱动器是否存在:
return Directory.Exists(sDrive);
这返回 True,但是当我进入资源管理器时,我没有看到驱动器。如果我转到 dos 提示符并使用 来检查驱动器net use
,我看不到它。因此,尽管 Directory.Exists 返回 True,但我看不到任何映射的驱动器。这似乎没有失败,我想在某种程度上,它不是。否则 Directory.Exists 不会返回 True。
更糟糕的是,这是旧 VB.NET 实用程序的更新版本。该实用程序至少对我有用,尽管有人抱怨它不起作用。它的 VB.NET 代码是:
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)
除了他们使用不同的编程语言这一事实之外,我看不出这两者之间有任何区别。
查看 Windows 7 中的注册表,我看到映射在 HKCU\Network 下的驱动器。当我查看 Windows 10 机器时,我看到没有映射驱动器。所以这是一个区别。
另一个区别 - 如果我使用提升的命令提示符,那么我可以看到使用 net use 的驱动器,并且我可以通过 DOS 访问它们。
因此,似乎驱动器正在被映射并且可以从提升的命令提示符访问。
我错过了什么?还有其他我应该处理的方法吗?我曾经使用 WNetAddConnection,但在 Windows 10 上停止使用它,因为它似乎不起作用。