2013-02-21 22 views
4

我在asp.net的MVC项目的XML文件(resource.xml)和T4文件(resource.tt)将该文件转换到JSON在.js文件(资源。 JS)。如何在另一个文件更改时自动生成文件?

的问题是我要自动运行文件T4文件resource.xml改变或保存时。

我知道,在asp.net有的.resx文件,当它改变时,自定义工具会自动生成一个文件,

我想类似的东西

更新: 在我的项目,我有/Resources/Resource.fr.xml一个XML文件和读取XML文件,并在/Resources/Resource.fr.js文件生成JSON对象T4文件。 我想在tml文件保存或更改xml文件时生成.js文件。

+0

这是你贴在这里另外一个问题重复http://stackoverflow.com/questions/14997308/how-can-set-the-custom- tool-property-of-an-xml-file-to-a-t4-file/15037150#15037150 – 2013-02-23 04:48:07

回答

1

看看FileSystemWatcher类。它监视对文件甚至文件夹的更改。

看看这个例子:

using System; 使用System.IO; using System.Security.Permissions;

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Run(@"C:\Users\Hanlet\Desktop\Watcher\ConsoleApplication1\bin\Debug"); 
     } 
     [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] 
     public static void Run(string path) 
     { 

      FileSystemWatcher watcher = new FileSystemWatcher(); 
      watcher.Path =path; 
      watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
       | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
      watcher.Filter = "*.xml"; 

      watcher.Changed += new FileSystemEventHandler(OnChanged); 
      watcher.Created += new FileSystemEventHandler(OnChanged); 
      watcher.Deleted += new FileSystemEventHandler(OnChanged); 

      watcher.EnableRaisingEvents = true; 

      Console.WriteLine("Press \'q\' to quit the sample."); 
      while (Console.Read() != 'q') ; 
     } 

     private static void OnChanged(object source, FileSystemEventArgs e) 
     { 
      if(e.FullPath.IndexOf("resource.xml") > - 1) 
       Console.WriteLine("The file was: " + e.ChangeType); 
     } 
    } 
} 

这种显示器和卡子每次所述resource.xml文件遭受某种变化(创建,删除或更新)。祝你好运!

+0

你能给我更多的细节吗?我如何在Visual Studio项目中实现它并扫描我的文件? – 2013-02-21 08:13:08

+0

谢谢,请告诉我,如果我可以运行此代码时Visual Studio构建/文件保存? – 2013-02-25 10:15:47

2

我刚才已经回答这类问题在这个thread

检查了这一点:https://github.com/thomaslevesque/AutoRunCustomTool或 https://visualstudiogallery.msdn.microsoft.com/ecb123bf-44bb-4ae3-91ee-a08fc1b9770e 自述:
您安装该扩展后,你会看到每个项目项物业新的运行自定义工具。只需编辑此属性即可添加目标文件的名称。而已!
“目标”的文件是你.TT文件
+0

这应该是被接受的答案 – flakes 2016-07-11 22:34:54

相关问题