簡體   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