Windows文件变更监控
来源:互联网 发布:网络玄幻小说典藏版27 编辑:程序博客网 时间:2024/04/30 01:22
有的时候需要在新建、修改和删除文件时执行一些操作,这就需要监控文件的变更。http://zhanyonhu.blog.163.com/blog/static/16186044200882484143208/提出了3种方法来实现:
1. 函数FindFirstChangeNotification和FindNextChangeNotification
2. 系统核心工具软件,比如FileMon(文件监视),开放源代码
3. Hook API,钩住CreateFile函数,分析其参数,借此来判断是创建文件还是读写文件;钩住DeleteFile函数,监视文件删除操作;钩住CreateDirectory和RemoveDirectory,借此判断是否是删除文件;钩住回收站相关函数,借此判断是否是移动到回收站。
后面两种方法一个使用驱动相关的技术,一个使用Hook,都比较复杂。通过Windows提供的API已经可以满足一些基本的文件监控需求。下面主要看下2个函数:
FindFirstChangeNotification Function
Creates a change notification handle and sets up initial change notification filter conditions. A wait on a notification handle succeeds when a change matching the filter conditions occurs in the specified directory or subtree. The function does not report changes to the specified directory itself.
This function does not indicate the change that satisfied the wait condition. To retrieve information about the specific change as part of the notification, use the ReadDirectoryChangesW function.
HANDLE WINAPI FindFirstChangeNotification(
__in LPCTSTR lpPathName,
__in BOOL bWatchSubtree,
__in DWORD dwNotifyFilter
);
Parameters
lpPathName
The path of the directory to be watched.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.
bWatchSubtree
If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory; if it is FALSE, it monitors only the specified directory.
dwNotifyFilter
The filter conditions that satisfy a change notification wait. This parameter can be one or more of the following values.
http://www.codeproject.com/KB/files/DirCheck.aspx提供了一个用FindFirstChangeNotification实现的封装类。
ReadDirectoryChangesW Function
Retrieves information that describes the changes within the specified directory. The function does not report changes to the specified directory itself.
To track changes on a volume, see change journals.
Syntax
BOOL WINAPI ReadDirectoryChangesW(
__in HANDLE hDirectory,
__out LPVOID lpBuffer,
__in DWORD nBufferLength,
__in BOOL bWatchSubtree,
__in DWORD dwNotifyFilter,
__out_opt LPDWORD lpBytesReturned,
__inout_opt LPOVERLAPPED lpOverlapped,
__in_opt LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
);
Parameters
hDirectory [in]
A handle to the directory to be monitored. This directory must be opened with the FILE_LIST_DIRECTORY access right.
lpBuffer [out]
A pointer to the DWORD-aligned formatted buffer in which the read results are to be returned. The structure of this buffer is defined by the FILE_NOTIFY_INFORMATION structure. This buffer is filled either synchronously or asynchronously, depending on how the directory is opened and what value is given to the lpOverlapped parameter. For more information, see the Remarks section.
nBufferLength [in]
The size of the buffer that is pointed to by the lpBuffer parameter, in bytes.
bWatchSubtree [in]
If this parameter is TRUE, the function monitors the directory tree rooted at the specified directory. If this parameter is FALSE, the function monitors only the directory specified by the hDirectory parameter.
dwNotifyFilter [in]
The filter criteria that the function checks to determine if the wait operation has completed. This parameter can be one or more of the following values.
lpBytesReturned [out, optional]
For synchronous calls, this parameter receives the number of bytes transferred into the lpBuffer parameter. For asynchronous calls, this parameter is undefined. You must use an asynchronous notification technique to retrieve the number of bytes transferred.
lpOverlapped [in, out, optional]
A pointer to an OVERLAPPED structure that supplies data to be used during asynchronous operation. Otherwise, this value is NULL. The Offset and OffsetHigh members of this structure are not used.
lpCompletionRoutine [in, optional]
A pointer to a completion routine to be called when the operation has been completed or canceled and the calling thread is in an alertable wait state. For more information about this completion routine, see FileIOCompletionRoutine.
http://www.codeproject.com/KB/files/directorychangewatcher.aspx提供了一个用ReadDirectoryChangesW实现的封装类。
http://www.codeproject.com/KB/files/FileSpyArticle.aspx给出了FindFirstChangeNotification和ReadDirectoryChangesW的一些详细介绍和示例。可以参考一下。
其他相关的解决方案:
FileSystemWatcher侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。可监视指定目录中的文件或子目录的更改。可以创建一个组件来监视本地计算机、网络驱动器或远程计算机上的文件。
下面的示例创建一个 FileSystemWatcher,以便在运行时监视指定的目录。组件设置为监视 LastWrite 和 LastAccess 时间方面的更改,以及目录中文本文件的创建、删除或重命名。如果更改、创建或删除文件,文件路径将被输出到控制台。在文件重命名后,旧路径和新路径都输出到控制台。在此示例中使用 System.Diagnostics 和 System.IO 命名空间。
- <pre class="cpp" name="code">public ref class Watcher
- {
- private:
- // Define the event handlers.
- static void OnChanged( Object^ /*source*/, FileSystemEventArgs^ e )
- {
- // Specify what is done when a file is changed, created, or deleted.
- Console::WriteLine( "File: {0} {1}", e->FullPath, e->ChangeType );
- }
- 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 );
- }
- public:
- [PermissionSet(SecurityAction::Demand, Name="FullTrust")]
- int static run()
- {
- array<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 0;
- }
- // Create a new FileSystemWatcher and set its properties.
- FileSystemWatcher^ watcher = gcnew FileSystemWatcher;
- watcher->Path = args[ 1 ];
- /* Watch for changes in LastAccess and LastWrite times, and
- the renaming of files or directories. */
- watcher->NotifyFilter = static_cast<NotifyFilters>(NotifyFilters::LastAccess |
- NotifyFilters::LastWrite | NotifyFilters::FileName | NotifyFilters::DirectoryName);
- // Only watch text files.
- watcher->Filter = "*.txt";
- // Add event handlers.
- watcher->Changed += gcnew FileSystemEventHandler( Watcher::OnChanged );
- watcher->Created += gcnew FileSystemEventHandler( Watcher::OnChanged );
- watcher->Deleted += gcnew FileSystemEventHandler( Watcher::OnChanged );
- watcher->Renamed += gcnew RenamedEventHandler( Watcher::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' )
- ;
- }
- };
- int main() {
- Watcher::run();
- }</pre>
- <pre></pre>
- <pre></pre>
- FileSyncProvider是同步的同步提供程序,它可用于同步 NTFS、FAT 或 SMB 文件系统中的文件、文件夹和子文件夹。详细参见MSDN<a href="http://msdn.microsoft.com/zh-cn/library/microsoft.synchronization.files.filesyncprovider(v=SQL.100).aspx">http://msdn.microsoft.com/zh-cn/library/microsoft.synchronization.files.filesyncprovider(v=SQL.100).aspx</a><br>
- <br>
- <pre></pre>
- <pre></pre>
- Windows文件变更监控
- Windows文件变更监控
- linux使用Inotify监控目录或者文件状态变更
- linux使用Inotify监控目录或者文件状态变更
- windows 监控文件目录
- windows 文件监控
- 文件监控 Linux Windows
- windows文件监控
- windows 监控文件目录
- 监控联系人变更
- 监控通话记录的变更
- VC 实现Windows 文件监控
- Windows DOS命令按变更时间复制文件脚本
- 文件监控系统设计(2)-"Windows 文件监控API"
- .NET4 Windows Service 监控磁盘文件
- Python监控Windows下的文件变化
- Windows监控文件变化(ReadDirectoryChangesW)
- 【Windows】gpedit.msc--监控文件操作
- 画箭头
- Android 平台未安装邮件客户端,点击邮件链接,程序崩溃问题
- 虚指针的用法(原出处//http://blog.csdn.net/haoel/article/details/1948051)
- zend studio 10.5破解方法
- uva10763 简单做法
- Windows文件变更监控
- iOS 7教程:浅析Text Kit
- IOS 一些免费得接口
- org.springframework.orm.hibernate3.HibernateSystemException: Null value was assigned to a property o
- TS流播放器 开源免费播放器VLC Media Player
- 关于varying的理解
- VC多线程编程之线程创建与示例
- iOS 7 SDK: 如何使用后台获取(Background Fetch)
- Android 设置无标题栏(全屏)的两种方式