我不知道如何以编程方式访问“$(TargetPath)”,我同意这可能是最好的解决方案。
但是,您提到的方法应该仍然可行,因为 OutputPath 属性与项目文件所在的文件夹相关。(如果我错过了一些不是这种情况的场景,请告诉我?)
所以你可以做类似的事情:
private static string GetProjectExecutable(Project startupProject, Configuration config)
{
string projectFolder = Path.GetDirectoryName(startupProject.FileName);
string outputPath = (string)config.Properties.Item("OutputPath").Value;
string assemblyFileName = (string)startupProject.Properties.Item("AssemblyName").Value + ".exe";
return Path.Combine(new[] {
projectFolder,
outputPath,
assemblyFileName
});
}
(此处使用的 Path.Combine 重载仅在 .NET 4.0 中可用,但您始终可以向后移植它)