How to get actual project output using microsoft.build.evaluation.project in c#

馋奶兔 提交于 2019-12-13 17:32:54

问题


I need to know the actual output file name full path in a project. I use microsoft.build.evaluation.project to open a project with the correct properties, and i tried using the properties TargetPath, TargetDir, TargetFileName, but i can't find a consistent property to give me the output file name with the path. In some projects it works, and in some it doesn't. I work both on csproj files and vcxproj files.


回答1:


Output path is related to project folder. You can get its value from project file via property OutputPath (suppose project is an instance of Microsoft.Build.Evaluation.Project)

var outputPath = project.GetPropertyValue("OutputPath")

Then you can use Path.Combine to concatenate your output path and project folder

var fullpath =  Path.Combine(projectDirectory, outputPath);



回答2:


In addition to Ask's answer:

If you need to get an OutputPath for some other configuration than the current one (Release for example), you should do next:

project.SetProperty("Configuration", "Release");
project.ReevaluateIfNecessary();
var outputPath = project.GetPropertyValue("OutputPath");

or

ProjectCollection.GlobalProjectCollection.SetGlobalProperty("Configuration", "Release");
load project
var outputPath = project.GetPropertyValue("OutputPath");


来源:https://stackoverflow.com/questions/26139757/how-to-get-actual-project-output-using-microsoft-build-evaluation-project-in-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!