当我生成一个C#项目(
当我手动将预构建和后构建事件配置移动到生成的
将buildevent添加到项目中是我保存项目之前做的最后一件事情。
这是我添加的方式:
运行通过msbuild生成的项目时,我遇到的错误是这样的:
当我用记事本或其他文本编辑器手动编辑
什么是将生成事件添加到
显然,我当前使用的方法是从AddPropertyGroup请求
csproj
文件)并编译它时,msbuild
无法在预构建和后构建事件中识别变量$(ConfigurationName)
和$(ProjectDir)
(以及其他变量)。当我手动将预构建和后构建事件配置移动到生成的
.csproj
文件的下方时,然后msbuild会正确识别这些变量。将buildevent添加到项目中是我保存项目之前做的最后一件事情。
这是我添加的方式:
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
元素之后?显然,我当前使用的方法是从AddPropertyGroup请求
[ProjectPropertyGroupElement]
,并将其作为Xml属性的参数,由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();
}
}
重现步骤
- 创建一个新项目
- 手动添加 app.config.debug 文件,该文件应与 app.debug 文件不同
- 添加 postbuildevent:
copy "$(ProjectDir)app.config.$(ConfigurationName)" "$(TargetDir)\$(ProjectName).exe.config
- 检查项目构建并应用正确的配置文件
- 使用记事本删除 pre- 和 postbuild 事件(这样不会留下任何痕迹)
- 运行示例项目
- 重新加载并构建您创建的项目。
- 输出窗口将显示
The system cannot find the file specified.
app.config
不存在(缺少ConfigurationName
)。你的手动构建事件是什么? - Patrick Hofman$(projectdir)
也没有被解析。我尝试了这个:echo $(ProjectDir) $(Configuration) $(TargetDir)
,它有所帮助,$(Configuration)
得到了解析,但另外两个没有。 - Alfons