如何判断文件是否已通过.NET更改?

时间:2022-11-05 13:57:54

I am looking for a way to determine when a particular file has changed, through .NET. (What I ultimately want is functionality that makes a copy of the file as soon as it has changed.) How can this be done?

我正在寻找一种方法来确定特定文件何时更改,通过.NET。 (我最终想要的是在文件发生变化后立即复制文件的功能。)如何做到这一点?

6 个解决方案

#1


class Program
    {
        static void Main(string[] args)
        {
            FileSystemWatcher fsw = new FileSystemWatcher(@"c:\temp");
            fsw.Changed += new FileSystemEventHandler(fsw_Changed);
            fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
            fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
            fsw.Created += new FileSystemEventHandler(fsw_Created);
            fsw.EnableRaisingEvents = true;
            Console.ReadLine();
        }

        static void fsw_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("{0} was created", e.FullPath);
        }

        static void fsw_Renamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine("{0} was Renamed", e.FullPath);
        }

        static void fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("{0} was Deleted", e.FullPath);
        }

        static void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("{0} was Changed", e.FullPath);
        }
}

#2


You can use a FileSystemWatcher object. This raises events on changes to files within the specified watched folder.

您可以使用FileSystemWatcher对象。这会在指定监视文件夹中的文件更改时引发事件。

#3


Microsoft Windows, and its ultimate ancestor MS-DOS, has always had an attribute on its files that indicates whether that file was changed since the last time that attribute was cleared, sort of a "dirty flag". It was used in the past by backup programs to find those files that needed to be backed up incrementally, and then cleared when a copy of that file had been made.

Microsoft Windows及其最终祖先MS-DOS始终在其文件上有一个属性,指示自上次清除该属性后该文件是否已更改,这是一种“脏标志”。过去,备份程序使用它来查找需要逐步备份的文件,然后在创建该文件的副本时将其清除。

You can use File.GetAttributes to get the attributes on a file, and use File.SetAttributes to clear that "Archive" attribute. The next time that file is opened for writing, that archive flag will be set again.

您可以使用File.GetAttributes获取文件的属性,并使用File.SetAttributes清除“Archive”属性。下次打开该文件进行写入时,将再次设置该归档标志。

Be careful about copying files that have been changed, as those files may still be open. You probably want to avoid concurrency problems by opening them for read exclusively when copying, and if that fails you know that file is still open for writing.

请注意复制已更改的文件,因为这些文件可能仍处于打开状态。您可能希望通过在复制时将它们打开以进行读取来避免并发问题,如果失败则您知道该文件仍然可以写入。

#4


To all who responded with, use FileSystemWatcher, how do you deal with the time your app isn't running? For example, the user has rebooted the box, modified the file you are interested in, and then starts up your app?

对于所有回复的人,使用FileSystemWatcher,您如何处理应用程序未运行的时间?例如,用户重新启动了该框,修改了您感兴趣的文件,然后启动了您的应用程序?

Be sure to also read the docs on FileSystemWatcher Class carefully specially the section about Events and Buffer Sizes.

请务必仔细阅读有关事件和缓冲区大小的文章,详细阅读有关FileSystemWatcher类的文档。

#5


You're likely to run into issues with FileSystemWatcher (not getting events, too many events, etc.) This code wraps it up and solves many of them: http://precisionsoftware.blogspot.com/2009/05/filesystemwatcher-done-right.html

您可能会遇到FileSystemWatcher的问题(没有获取事件,太多事件等)。这段代码包含了它并解决了许多问题:http://precisionsoftware.blogspot.com/2009/05/filesystemwatcher-done -right.html

#6


You will have to note the modified date of files which you want to check. After certain period you can check if the file was modified later. If the file is modified with different date and time, you can make the copy.

您必须记下要检查的文件的修改日期。在一段时间后,您可以检查文件是否稍后被修改。如果使用不同的日期和时间修改文件,则可以进行复制。

#1


class Program
    {
        static void Main(string[] args)
        {
            FileSystemWatcher fsw = new FileSystemWatcher(@"c:\temp");
            fsw.Changed += new FileSystemEventHandler(fsw_Changed);
            fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
            fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
            fsw.Created += new FileSystemEventHandler(fsw_Created);
            fsw.EnableRaisingEvents = true;
            Console.ReadLine();
        }

        static void fsw_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("{0} was created", e.FullPath);
        }

        static void fsw_Renamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine("{0} was Renamed", e.FullPath);
        }

        static void fsw_Deleted(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("{0} was Deleted", e.FullPath);
        }

        static void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("{0} was Changed", e.FullPath);
        }
}

#2


You can use a FileSystemWatcher object. This raises events on changes to files within the specified watched folder.

您可以使用FileSystemWatcher对象。这会在指定监视文件夹中的文件更改时引发事件。

#3


Microsoft Windows, and its ultimate ancestor MS-DOS, has always had an attribute on its files that indicates whether that file was changed since the last time that attribute was cleared, sort of a "dirty flag". It was used in the past by backup programs to find those files that needed to be backed up incrementally, and then cleared when a copy of that file had been made.

Microsoft Windows及其最终祖先MS-DOS始终在其文件上有一个属性,指示自上次清除该属性后该文件是否已更改,这是一种“脏标志”。过去,备份程序使用它来查找需要逐步备份的文件,然后在创建该文件的副本时将其清除。

You can use File.GetAttributes to get the attributes on a file, and use File.SetAttributes to clear that "Archive" attribute. The next time that file is opened for writing, that archive flag will be set again.

您可以使用File.GetAttributes获取文件的属性,并使用File.SetAttributes清除“Archive”属性。下次打开该文件进行写入时,将再次设置该归档标志。

Be careful about copying files that have been changed, as those files may still be open. You probably want to avoid concurrency problems by opening them for read exclusively when copying, and if that fails you know that file is still open for writing.

请注意复制已更改的文件,因为这些文件可能仍处于打开状态。您可能希望通过在复制时将它们打开以进行读取来避免并发问题,如果失败则您知道该文件仍然可以写入。

#4


To all who responded with, use FileSystemWatcher, how do you deal with the time your app isn't running? For example, the user has rebooted the box, modified the file you are interested in, and then starts up your app?

对于所有回复的人,使用FileSystemWatcher,您如何处理应用程序未运行的时间?例如,用户重新启动了该框,修改了您感兴趣的文件,然后启动了您的应用程序?

Be sure to also read the docs on FileSystemWatcher Class carefully specially the section about Events and Buffer Sizes.

请务必仔细阅读有关事件和缓冲区大小的文章,详细阅读有关FileSystemWatcher类的文档。

#5


You're likely to run into issues with FileSystemWatcher (not getting events, too many events, etc.) This code wraps it up and solves many of them: http://precisionsoftware.blogspot.com/2009/05/filesystemwatcher-done-right.html

您可能会遇到FileSystemWatcher的问题(没有获取事件,太多事件等)。这段代码包含了它并解决了许多问题:http://precisionsoftware.blogspot.com/2009/05/filesystemwatcher-done -right.html

#6


You will have to note the modified date of files which you want to check. After certain period you can check if the file was modified later. If the file is modified with different date and time, you can make the copy.

您必须记下要检查的文件的修改日期。在一段时间后,您可以检查文件是否稍后被修改。如果使用不同的日期和时间修改文件,则可以进行复制。