以管理员身份运行时出现"File Not Found"程序错误



我有一个用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=@"\servershare";(除了使用你的服务器和共享…)

然后

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,但请使用您的判断。

最新更新