我想在 USB 中打开文件夹时创建一个自动日志文件。 在 Windows 中,双击或从 USB 驱动器打开文件夹时如何运行脚本。
就像这个 autorun.sh
我建议你编写一个 Windows Service 来挂钩文件夹打开。
这是一个拦截文件夹打开的独立应用程序的示例:
#include <Windows.h>
HHOOK g_hookHandle = NULL;
LRESULT CALLBACK FolderOpenHook(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HSHELL_WINDOWCREATED) {
HWND hWnd = (HWND)wParam;
// Check if the newly created window belongs to Windows Explorer
TCHAR className[256];
if (GetClassName(hWnd, className, sizeof(className)) > 0) {
if (_tcsicmp(className, _T("CabinetWClass")) == 0) {
// Handle the folder opening event here
// You can perform your custom actions...
}
}
}
return CallNextHookEx(g_hookHandle, nCode, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
g_hookHandle = SetWindowsHookEx(WH_SHELL, FolderOpenHook, hInstance, 0);
if (!g_hookHandle) {
// Handle hook setup failure
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(g_hookHandle);
return (int)msg.wParam;
}
这里有一个 C++ 中的简单 Windows 服务创建指南。