每次我保存我在 NotePad++ 中处理的某个脚本文件时,我都需要将更改上传到我们的服务器,以便我们可以将更改部署到各种机器。
在 NotePad++ 中重构我的代码后,我有时会忘记上传更改,我想知道是否有办法创建一个简单的应用程序来监听“保存”事件并自动为我上传文件。
我目前在 Windows 操作系统上运行,并希望使用 C++ 来执行此操作。我想探索 Windows 事件并可能绑定(bind)到事件 Hook 中来完成此操作。也欢迎任何其他语言。
有什么想法或提示吗?
到目前为止,这是我遵循 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
过滤器监听事件;这将在写入受监视目录中的文件时释放您的等待。
请注意,并非所有程序都以相同的方式保存文件;一些打开现有文件并写入,另一些删除它并替换,或它们的某种组合(首先写入临时文件,然后将其与原始文件交换)。因此,它可能不像等待上次写入通知那样直接只是来准确完成您所追求的。
关于c++ - 已保存文件的 Windows 事件 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40894106/