How can I generate a file automatically when another file changes?

I have an xml file (resource.xml) in my asp.net mvc project and a T4 file (resource.tt) to convert this file to json in a .js file (resource.js).

The problem is that I want to automatically run the t4 file when changing or saving the resource.xml file.

I know that asp.net has a .resx file that, when modified, sets up a tool to automatically create the file,

I want something like this

Update: In my project, I have an XML file in /Resources/Resource.fr.xml and a t4 file, read the XML file and generate a JSON object in /Resources/Resource.fr.js . I want the t4 file to generate a .js file when the xml file is saved or modified.

+4
source share
2 answers

I just answered this question in this thread

Check this out: https://github.com/thomaslevesque/AutoRunCustomTool or https://visualstudiogallery.msdn.microsoft.com/ecb123bf-44bb-4ae3-91ee-a08fc1b9770e From the readme file:
After installing the extension, you should see a new custom Run tool for each element of the project. Just edit this property to add the name (s) of the target file (s). It!
"target" files are your .tt files
+2
source

Take a look at the FileSystemWatcher class. It tracks changes to a file or even a folder.

Take a look at this example:

using System; using 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); } } } 

This tracks and catches every time the resource.xml file has some kind of change (created, deleted or updated). Good luck

+1
source

All Articles