繁体   English   中英

以管理员身份运行时出现“找不到文件”的程序错误

[英]Program errors with “File Not Found” when Run As Administrator

我有一个用C#构建的程序。 它将文件从网络驱动器复制到您的桌面。

string desktop = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
File.Copy("T:\\DATS Launcher.exe", desktop + "\\DATS Launcher.exe", true);

如果我正常运行该程序,它将正常工作。

如果使用“以管理员身份运行”运行程序,则会得到:

************** Exception Text **************

System.IO.DirectoryNotFoundException: Could not find a part of the path 'T:\DATS Launcher.exe'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

为什么会发生这种情况?

当您以管理员身份运行时,不会映射T:驱动器,因为它以其他用户身份运行。

因此,您应该使用T:驱动器的UNC路径,而不是驱动器名称。

T:似乎是仅为当前用户安装的网络驱动器。

您还可以执行以下操作:

设置string Letter="T"; string Path=@"\\\\server\\share"; (除了使用您的服务器并共享...)

然后

ProcessStartInfo psi=new ProcessStartInfo(
    "net.exe",
    "use "+Letter+" \""+Path+"\" /persistent:yes");
psi.CreateNoWindow=true; // We don't need a console showing up for this
psi.UseShellExecute=false; // Most likely optional. Required only if you want to
    // mess with the standard input/output of the process.
    // (for example, to check if mapping was successful).

Process prc=Process.Start(psi);

您可能还希望设置/persistent:no如果它是一次性使用的应用程序),但是请根据您的判断。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM