Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当另一个文件发生更改时,如何自动生成文件?_C#_Asp.net_Visual Studio_T4_Resx - Fatal编程技术网

C# 当另一个文件发生更改时,如何自动生成文件?

C# 当另一个文件发生更改时,如何自动生成文件?,c#,asp.net,visual-studio,t4,resx,C#,Asp.net,Visual Studio,T4,Resx,在我的asp.net mvc项目中有一个xml文件(resource.xml),还有一个T4文件(resource.tt),用于将该文件转换为.js文件(resource.js)中的json 问题是我想在resource.xml文件更改或保存时自动运行t4文件 我知道asp.net中有一个.resx文件,当它更改时,自定义工具会自动生成一个文件 我想要那样的东西 更新: 在我的项目中,我在/Resources/Resource.fr.xml中有一个xml文件,在/Resources/Resour

在我的asp.net mvc项目中有一个xml文件(resource.xml),还有一个T4文件(resource.tt),用于将该文件转换为.js文件(resource.js)中的json

问题是我想在resource.xml文件更改或保存时自动运行t4文件

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

我想要那样的东西

更新: 在我的项目中,我在/Resources/Resource.fr.xml中有一个xml文件,在/Resources/Resource.fr.js文件中有一个t4文件,用于读取xml文件并生成json对象。
我想在保存或更改xml文件时生成.js文件。

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

看看这个例子:

使用制度; 使用System.IO; 使用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文件发生某种更改(创建、删除或更新)时,它都会监视和捕获。祝你好运

我刚刚在这篇文章中回答了这类问题

看看这个:https://github.com/thomaslevesque/AutoRunCustomTool 或 https://visualstudiogallery.msdn.microsoft.com/ecb123bf-44bb-4ae3-91ee-a08fc1b9770e 自述文件: 安装扩展后,应该会在每个项目项上看到一个新的Run custom tool on属性。只需编辑此属性即可添加目标文件的名称。就这样! “目标”文件是您的.tt文件
你能告诉我更多的细节吗?如何在visual studio项目中实现它并保存我的文件?谢谢,请告诉我在visual studio生成/保存文件时如何运行这些代码?这是您在此处发布的另一个问题的副本