有什么方法可以让我知道文件是否已被修改?
is there any way for me to know if a file has been modified?
我正在制作一个程序来检测文件是否已被修改。但文件不是我的。我想知道是否有可能知道我得到的文件是否已经被修改过。
我有办法知道吗?我试过创建日期和修改日期,但有时当我修改文件时,它们的值是相同的。
P.S。我没有原始文件。我想知道是否有可能在我得到它之前知道我得到的是否没有改变。
以下示例创建一个 FileSystemWatcher 以监视在 运行 时间指定的目录。该组件设置为监视 LastWrite 和 LastAccess 时间的变化,以及目录中文本文件的创建、删除或重命名。如果文件被更改、创建或删除,该文件的路径将打印到控制台。重命名文件时,旧路径和新路径将打印到控制台。
本示例使用 System.Diagnostics 和 System.IO 命名空间。
using System;
using System.IO;
using System.Security.Permissions;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
如果您想知道文件的内容是否发生了变化,请计算其哈希值:
using System.IO;
using System.Security.Cryptography;
using (HashAlgorithmalgorithm = new .SHA512Managed())
{
using (Stream fileStream = new FileStream(@"path", FileMode.Open))
{
byte[] hash = algorithm.ComputeHash(fileStream);
}
}
在第一个 运行 上随心所欲地保留哈希,然后重新计算它并将其与保存的值相匹配。如果它们不同,则文件已更改。
我认为这个问题有几个问题,因为有多种方法可以回答这个问题。
1) 如果问题是 "I have received a file (that previously was not on the computer/network/location), and want to know if it has been modified...",那么答案是 - 不,因为您正在查看的只是一大块位,那么所有这些位都可能会被修改和调整以发送者想要的任何方式——时间戳、file-status 指示符(例如,Windows 系统上的 'archive' 标记,或较新文件系统上的结构化元数据)或关于该文件的任何其他内容都可以设置成他们想要的。
2) 如果问题是 "I have a file (on the local network or file-system that I can 'poke' every so often, or locally on the drive that I can 'poke' every so often) and I want to know if it has been modified between the times my application has run",那么最简单的方法可能是存储该文件的计算哈希值(如@Albireo 所回答)。
3) 如果问题是 "I have a file (on the local network or file-system) and I want to know if it was modified prior to any running of my application",那么您正在寻找可能或不可能的东西,并且不可能以完全可靠的方式实现 - 对底层有一些了解结构和您对该文件的期望是必要的,您最好更新您的问题,详细说明文件的组成以及您为什么要以编程方式查找更改,以便我们确定最佳方法这样做。
我正在制作一个程序来检测文件是否已被修改。但文件不是我的。我想知道是否有可能知道我得到的文件是否已经被修改过。
我有办法知道吗?我试过创建日期和修改日期,但有时当我修改文件时,它们的值是相同的。
P.S。我没有原始文件。我想知道是否有可能在我得到它之前知道我得到的是否没有改变。
以下示例创建一个 FileSystemWatcher 以监视在 运行 时间指定的目录。该组件设置为监视 LastWrite 和 LastAccess 时间的变化,以及目录中文本文件的创建、删除或重命名。如果文件被更改、创建或删除,该文件的路径将打印到控制台。重命名文件时,旧路径和新路径将打印到控制台。 本示例使用 System.Diagnostics 和 System.IO 命名空间。
using System;
using System.IO;
using System.Security.Permissions;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if(args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
如果您想知道文件的内容是否发生了变化,请计算其哈希值:
using System.IO;
using System.Security.Cryptography;
using (HashAlgorithmalgorithm = new .SHA512Managed())
{
using (Stream fileStream = new FileStream(@"path", FileMode.Open))
{
byte[] hash = algorithm.ComputeHash(fileStream);
}
}
在第一个 运行 上随心所欲地保留哈希,然后重新计算它并将其与保存的值相匹配。如果它们不同,则文件已更改。
我认为这个问题有几个问题,因为有多种方法可以回答这个问题。
1) 如果问题是 "I have received a file (that previously was not on the computer/network/location), and want to know if it has been modified...",那么答案是 - 不,因为您正在查看的只是一大块位,那么所有这些位都可能会被修改和调整以发送者想要的任何方式——时间戳、file-status 指示符(例如,Windows 系统上的 'archive' 标记,或较新文件系统上的结构化元数据)或关于该文件的任何其他内容都可以设置成他们想要的。
2) 如果问题是 "I have a file (on the local network or file-system that I can 'poke' every so often, or locally on the drive that I can 'poke' every so often) and I want to know if it has been modified between the times my application has run",那么最简单的方法可能是存储该文件的计算哈希值(如@Albireo 所回答)。
3) 如果问题是 "I have a file (on the local network or file-system) and I want to know if it was modified prior to any running of my application",那么您正在寻找可能或不可能的东西,并且不可能以完全可靠的方式实现 - 对底层有一些了解结构和您对该文件的期望是必要的,您最好更新您的问题,详细说明文件的组成以及您为什么要以编程方式查找更改,以便我们确定最佳方法这样做。