简体   繁体   English

C# - 设置新创建的 xml 文件的构建操作的代码

[英]C# - Code to set build action of newly created xml file

I have code that creates a new Xml file and then I simply save it to a location in another project.我有创建新 Xml 文件的代码,然后我只需将其保存到另一个项目中的某个位置。 My question is, is there code that I can write so that:我的问题是,是否有我可以编写的代码:

  1. My file gets included in my project.我的文件包含在我的项目中。

  2. Build Action gets set to Content.构建操作设置为内容。

  3. Set "Copy to Output Directory" to "Copy Always".将“复制到 Output 目录”设置为“始终复制”。

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

Try this... You may find better approaches possibly, but this one isn't bad (for me) and should work.试试这个......您可能会找到更好的方法,但是这个方法还不错(对我来说)并且应该可以工作。

The settings outlined in the question are typically entries in the project file (.csproj).问题中概述的设置通常是项目文件 (.csproj) 中的条目。 Try editing the.csproj file on the target location and add the entries.尝试在目标位置编辑 .csproj 文件并添加条目。

You will have to first search for a tag in csproj XML file, with tag name <ItemGroup> that has child tag(s) with name <Content> .您必须首先在 csproj XML 文件中搜索标签名称为<ItemGroup>的标签,该标签的子标签名称为<Content> If you do not find any, add an <ItemGroup> when you write your xml entries to the proj file.如果没有找到,请在将 xml 条目写入 proj 文件时添加一个<ItemGroup> If you find one, just add the tage part in the below string to that ItemGroup tag.如果您找到一个,只需将以下字符串中的标签部分添加到该 ItemGroup 标签。

@"<ItemGroup>" + // Add this parent node only if you do not find an existing one as mentioned above
"<Content Include="MyXmlFileNameWithExtemsionAndPath.xml">" +
"<CopyToOutputDirectory>Always</CopyToOutputDirectory></Content>" +
"</ItemGroup>" // Add this only if you do not find an existing one as mentioned above

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM