当我生成一个C#项目(csproj文件)并对其进行编译时,msbuild在构建前后事件中不知怎么不识别变量$(ConfigurationName)和$(ProjectDir) (以及其他变量)。
当我手动将生成的.csproj文件中的预构建事件配置和生成后事件配置进一步向下移动时,msbuild将正确识别这些变量。
将构建器添加到项目中是我在保存项目之前所做的最后一件事。
我是这样加起来的:
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
private const string PreBuildEventFixture = "PreBuildEvent";
private const string PostBuildEventFixture = "PostBuildEvent";
private const string PreBuildEvent = "attrib -R \"$(ProjectDir)app.config\"";
private const string PostBuildEvent = "copy \"$(ProjectDir)app.config.$(ConfigurationName)\" \"$(TargetDir)\\$(ProjectName).dll.config\" \r\n attrib -R \"$(ProjectPath)\"";
public void AddBuildEvents(Project project)
{
ProjectPropertyGroupElement propertyGroupElement = project.Xml.AddPropertyGroup();
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
}在通过msbuild运行生成的项目时,我遇到的错误如下:
The command "copy "app.config." "\.dll.config"" exited with code 1然后,当我手动编辑.csproj文件(使用记事本或其他文本编辑器)时,剪切编译前后的事件,并将其粘贴到<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />元素下面,然后msbuild就可以很好地构建生成的.csproj文件。
将构建事件添加到.csproj文件以便在生成的XML中的Import元素之后结束的最佳方法是什么?
显然,我现在通过从[ProjectPropertyGroupElement][1]属性的AddPropertyGroup中请求它而使用Microsoft.Build.Evaluation.Project的方法不是。
示例项目:
using System.IO;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
class Program
{
private const string PreBuildEventFixture = "PreBuildEvent";
private const string PostBuildEventFixture = "PostBuildEvent";
private const string PreBuildEvent = "attrib -R \"$(ProjectDir)app.config\"";
private const string PostBuildEvent = "copy \"$(ProjectDir)app.config.$(ConfigurationName)\" \"$(TargetDir)\\$(ProjectName).exe.config\" \r\n attrib -R \"$(ProjectPath)\"";
private const string ProjectFile = @"C:\test\TestProject\TestProject.csproj";
static void Main(string[] args)
{
if (!File.Exists(ProjectFile))
throw new FileNotFoundException("ProjectFile not found");
ProjectCollection collection = new ProjectCollection();
Project project = collection.LoadProject(ProjectFile);
ProjectPropertyGroupElement propertyGroupElement = project.Xml.AddPropertyGroup();
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);
project.Save();
collection.UnloadAllProjects();
}
}复制的步骤
copy "$(ProjectDir)app.config.$(ConfigurationName)" "$(TargetDir)\$(ProjectName).exe.configThe system cannot find the file specified.发布于 2014-09-03 09:25:30
var propertyGroupElement = project.Xml.CreatePropertyGroupElement();
project.Xml.AppendChild(propertyGroupElement);
propertyGroupElement.AddProperty(PreBuildEventFixture, PreBuildEvent);
propertyGroupElement.AddProperty(PostBuildEventFixture, PostBuildEvent);发布于 2014-09-01 12:48:58
如果在实际构建项目之前添加了与项目相关的宏(构建项目包括添加引用),则不解析它们。不用使用$(ProjectName),可以使用解决方案变量(已经存在)构造路径,如下所示:
copy "$(SolutionDir)ProjectName\app.config.$(Configuration)" "$(SolutionDir)ProjectName\bin\$(Configuration)\ProjectName.dll.config"
注意,ProjectName是硬编码项目的实际名称,但是由于您正在生成一个项目,这应该很容易添加。
https://stackoverflow.com/questions/25567949
复制相似问题