T4输出附加到现有文件 [英] T4 append output to existing file

查看:351
本文介绍了T4输出附加到现有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使一个 T4 模板输出与现有文件进行合并?

Is it possible to make a T4 template output to be merged with an existing file?

例如,如果一个T4模板生成本地化资源的XML文件,是否有可能给他们一些现有的资源文件合并?

For example, if a T4 template generates localization resource XML files, is it possible to merge them in some existing resource file?

推荐答案

您可以访问下面的字符串生成器中的T4采用以为GenerationEnvironment财产。因此,通过添加类似以下到您的T4,你应该能够得到一个可行的解决方案;

You can get access to the underlying string builder the the T4 uses thought the GenerationEnvironment property. So by adding something like the following to your T4 you should be able to get a workable solution;

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".txt" #>
<#@ Import Namespace="System.IO" #>

Line #<#= rand.Next(0, 100).ToString() #>
<# AppendFile(@"C:\Development\PodCastSync\test\test.txt"); #>

<#+
    Random rand = new Random();

    private void AppendFile(string filename)
    {
       File.AppendAllText(filename, GenerationEnvironment.ToString());
    }        
#>

如果你想阻止得到更新,您可以设置GenerationEnvironment到一个新的字符串生成器后保存的内容是阻止任何默认的备份文件输出。

If you want to stop the default backing file from getting updated you can set GenerationEnvironment to a new string builder after you save the contents to stop anything being output.

这篇关于T4输出附加到现有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆