问题
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