简体   繁体   English

当另一个文件发生变化时,如何自动生成文件?

[英]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 that file to json in a .js file (resource.js). 我在我的asp.net mvc项目中有一个xml文件(resource.xml)和一个T4文件(resource.tt),用于将该文件转换为.js文件(resource.js)中的json

The issue is i want to run t4 file automatically when resource.xml file changes or saved. 问题是我想在resource.xml文件更改或保存时自动运行t4文件。

I know that in asp.net has a .resx file that when it changes, a custom tool automatically generate a file, 我知道在asp.net中有一个.resx文件,当它发生变化时,自定义工具会自动生成一个文件,

I want something like that 我想要那样的东西

Update: In my project I have an xml file in /Resources/Resource.fr.xml and a t4 file that read the xml file and generate json object in /Resources/Resource.fr.js file. 更新:在我的项目中,我在/Resources/Resource.fr.xml中有一个xml文件和一个读取xml文件并在/Resources/Resource.fr.js文件中生成json对象的t4文件。 I want to t4 file generate the .js file when xml file saved or changes. 我想在保存或更改xml文件时t4文件生成.js文件。

I have just answered this sort of 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: 看看这个:https://github.com/thomaslevesque/AutoRunCustomTool或https://visualstudiogallery.msdn.microsoft.com/ecb123bf-44bb-4ae3-91ee-a08fc1b9770e来自自述文件:
After you install the extension, you should see a new Run custom tool on property on each project item. 安装扩展后,您应该在每个项目项的属性上看到一个新的运行自定义工具。 Just edit this property to add the name(s) of the target file(s). 只需编辑此属性即可添加目标文件的名称。 That's it! 而已!
"target" files are your .tt files “目标”文件是您的.tt文件

Take a look at the FileSystemWatcher class. 看一下FileSystemWatcher类。 It monitors changes to a file or even to a folder. 它监视文件或文件夹的更改。

Look at this example: 看看这个例子:

using System; 使用系统; using System.IO; 使用System.IO; using System.Security.Permissions; 使用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 monitors and catches everytime the resource.xml file suffers some kind of change (created, deleted or updated). 每当resource.xml文件遭受某种更改(创建,删除或更新)时,它都会监视和捕获。 Good luck! 祝好运!

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

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