[英]Windows Event Hooks for File Saved
每次我保存要在NotePad ++中工作的脚本文件时,都需要将更改上传到我们的服务器,以便我们可以将更改部署到各种计算机上。
在NotePad ++中重构代码后,有时我忘记上传更改,而我想知道是否有办法创建一个简单的应用程序来监听“保存”事件并自动为我上传文件。
我目前在Windows操作系统上运行,希望使用C ++来实现。 我想探索Windows事件,并可能绑定一个事件挂钩来完成此任务。 任何其他语言也将受到欢迎。
有什么想法或提示吗?
到目前为止,这是我的代码,遵循了Josh的以下建议:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
void RefreshDirectory(LPTSTR);
void WatchDirectory(LPTSTR);
void _tmain(int argc, TCHAR *argv[])
{
if (argc != 2)
{
_tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
return;
}
WatchDirectory(argv[1]);
}
void WatchDirectory(LPTSTR lpDir)
{
DWORD dwWaitStatus;
HANDLE dwChangeHandles[2];
TCHAR lpDrive[4];
TCHAR lpFile[_MAX_FNAME];
TCHAR lpExt[_MAX_EXT];
_tsplitpath_s(lpDir, lpDrive, 4, NULL, 0, lpFile, _MAX_FNAME, lpExt, _MAX_EXT);
lpDrive[2] = (TCHAR)'\\';
lpDrive[3] = (TCHAR)'\0';
// Watch the directory for file creation and deletion.
dwChangeHandles[0] = FindFirstChangeNotification(
lpDir, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_LAST_WRITE); // watch file name changes
if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
{
printf("\n ERROR: FindFirstChangeNotification function failed.\n");
ExitProcess(GetLastError());
}
// Make a final validation check on our handles.
if ((dwChangeHandles[0] == NULL))
{
printf("\n ERROR: Unexpected NULL from FindFirstChangeNotification.\n");
ExitProcess(GetLastError());
}
// Change notification is set. Now wait on both notification
// handles and refresh accordingly.
while (TRUE)
{
// Wait for notification.
printf("\nWaiting for notification...\n");
// Waits until the specified object is in the signaled state or
// the time-out interval elapses.
// Because our second parameter is set to INFINITE, the function will
// return only when the object is signaled.
dwWaitStatus = WaitForSingleObject(dwChangeHandles, INFINITE);
switch (dwWaitStatus)
{
// Our return value, WAIT_OBJECT_0 signifies that the first object
// signaled the event.
case WAIT_OBJECT_0:
// A file was created, renamed, or deleted in the directory.
// Refresh this directory and restart the notification.
RefreshDirectory(lpDir);
if (FindNextChangeNotification(dwChangeHandles[0]) == FALSE)
{
printf("\n ERROR: FindNextChangeNotification function failed.\n");
ExitProcess(GetLastError());
}
break;
case WAIT_TIMEOUT:
// A timeout occurred, this would happen if some value other
// than INFINITE is used in the Wait call and no changes occur.
// In a single-threaded environment you might not want an
// INFINITE wait.
printf("\nNo changes in the timeout period.\n");
break;
default:
printf("\n ERROR: Unhandled dwWaitStatus.\n");
ExitProcess(GetLastError());
break;
}
}
}
void RefreshDirectory(LPTSTR lpDir)
{
// This is where you might place code to refresh your
// directory listing, but not the subtree because it
// would not be necessary.
_tprintf(TEXT("Directory (%s) changed.\n"), lpDir);
}
您可以使用FindFirstChangeNotification
监视文件系统的更改。 当您调用此函数时,您将得到一个HANDLE
。 您可以使用WaitSingleObject
(或类似方法)在该句柄上等待。 等待返回时,您可以使用ReadDirectoryChanges
准确地了解发生了什么。 如果发生的任何事情与某个事件相匹配或您对文件的关心有所更改,则可以采取适当的措施...否则忽略该事件。
因为您将要等待(并因此阻塞了线程),所以如果您要使相关程序执行其他任何操作,则可能需要在辅助线程上执行此工作。
一个简单的开始方法可能是使用FILE_NOTIFY_CHANGE_LAST_WRITE
过滤器监听事件; 当写入受监视目录中的文件时,这将释放您的等待时间。
请注意,并非所有程序都以相同的方式保存文件。 一些打开现有文件并对其进行写入,其他一些将其删除并替换,或它们的某种组合(首先写入临时文件,然后将其与原始文件交换)。 因此它可能没有等待就在去年写的通知完成正是你追求的那样简单。
考虑改用NP ++插件。
您可以使用NPPN_FILESAVED
或NPPN_FILEBEFORESAVE
注册插件,以便在文件保存或即将保存时得到通知。
请查看此链接:
http://docs.notepad-plus-plus.org/index.php/Messages_And_Notifications
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.