我得到一個Win32Exception
File not found
試圖運行從下面的代碼C#解決方案的外部可執行文件(具有相關性)時。運行在C#解決方案外部可執行使用相對路徑
public static string TestMethod()
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = Path.Combine("dist", @"test.exe");
p.Start();
}
catch (Exception ex)
{
expMessage = ex.Message;
}
return expMessage;
}
備註:
- 時被指定爲
FileName
絕對路徑時也不例外。 - 在MS Visual Studio中
dist
子文件夾中的文件屬性設置爲以下和dist
目錄確實複製到輸出文件夾:Build action: Content
Always copy in output directory
- 我有一個嘗試
test.exe.config
文件如下,但沒有成功:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="dist"/>
</assemblyBinding>
</runtime>
</configuration>
編輯在Specifying a relative path其實際工作在這種情況下提出的唯一的解決辦法是最終提供由維亞切Smityukh結合3210重建的絕對路徑註釋的一個。但是,PavelPájaHalbich在下面的回答中指出,運行時似乎存在潛在的問題。從How can I get the application's path in a .NET console application?我發現使用下面的代碼基於Mr.Mindor的評論另一種解決方案:
string uriPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
string localPath = new Uri(uriPath).LocalPath;
string testpath = Path.Combine(localPath, "dist", @"test.exe");
現在,我不知道哪一個是考慮與窗口安裝的解決方案的未來部署的正確方法。
[指定相對路徑]的可能重複(https://stackoverflow.com/questions/5077475/specifying-a-relative-path) –