我得到一个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) –