鱼C论坛

 找回密码
 立即注册
查看: 662|回复: 1

C++ MFC文件夹监听

[复制链接]
发表于 2024-10-12 10:42:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
请用C++ MFC写一个文件夹内指定类型文件的监听程序,将新增的文件名置入List box控件
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-12 10:48:57 | 显示全部楼层
要在C++ MFC中实现文件夹监听,并将新增的特定类型文件名加入ListBox控件,可以使用Windows的文件系统改动通知功能。以下是一个基本的实现思路:

1. **创建MFC对话框应用**:
   - 使用MFC应用程序向导创建一个基于对话框的应用程序。
   - 在对话框上添加一个ListBox控件。

2. **使用`FindFirstChangeNotification`等API来监控文件夹**:
   - 使用`FindFirstChangeNotification`函数来注册文件夹变更通知。
   - 使用一个工作线程不断地调用`WaitForSingleObject`来等待文件夹的变化事件发生。

3. **响应文件夹变化**:
   - 在检测到文件夹内容变化时,使用`CFileFind`类查找新增的特定类型文件。
   - 将检测到的文件名添加到ListBox控件。

下面是一个简单的代码示例,用于展示基本实现:

  1. #include <afxwin.h>
  2. #include <afxdlgs.h>
  3. #include <afxcmn.h>
  4. #include <thread>
  5. #include <atomic>
  6. #include <string>

  7. class CMyDialog : public CDialogEx {
  8. public:
  9.     CListBox m_listBox;
  10.     std::atomic<bool> m_running;

  11.     CMyDialog() : CDialogEx(IDD_MYDIALOG), m_running(true) { }

  12.     BOOL OnInitDialog() override {
  13.         CDialogEx::OnInitDialog();
  14.         m_listBox.SubclassDlgItem(IDC_LIST1, this);

  15.         std::thread([this] {
  16.             WatchDirectory();
  17.         }).detach();

  18.         return TRUE;
  19.     }

  20.     void WatchDirectory() {
  21.         CString folderPath = _T("C:\\YourDirectory");
  22.         HANDLE hChange = FindFirstChangeNotification(
  23.             folderPath, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);

  24.         if (hChange == INVALID_HANDLE_VALUE) return;

  25.         while (m_running) {
  26.             DWORD waitStatus = WaitForSingleObject(hChange, INFINITE);
  27.             if (waitStatus == WAIT_OBJECT_0) {
  28.                 HandleDirectoryChange(folderPath);
  29.                 FindNextChangeNotification(hChange);
  30.             }
  31.         }
  32.         FindCloseChangeNotification(hChange);
  33.     }

  34.     void HandleDirectoryChange(LPCTSTR lpPath) {
  35.         CFileFind finder;
  36.         BOOL bWorking = finder.FindFile(std::wstring(lpPath) + _T("\\*.txt"));

  37.         while (bWorking) {
  38.             bWorking = finder.FindNextFile();
  39.             if (!finder.IsDots() && !finder.IsDirectory()) {
  40.                 m_listBox.AddString(finder.GetFileName());
  41.             }
  42.         }
  43.     }

  44.     ~CMyDialog() {
  45.         m_running = false;
  46.     }
  47. };

  48. class CMyApp : public CWinApp {
  49. public:
  50.     BOOL InitInstance() override {
  51.         CMyDialog dlg;
  52.         m_pMainWnd = &dlg;
  53.         INT_PTR nResponse = dlg.DoModal();
  54.         return FALSE;
  55.     }
  56. };

  57. BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
  58. END_MESSAGE_MAP()

  59. CMyApp theApp;
复制代码


### 注意事项:
- 确保替换`C:\\YourDirectory`和文件类型`*.txt`为你需要监听的路径和文件类型。
- `FindFirstChangeNotification`对文件夹的子目录不递归,需要自行处理。
- 代码中使用了简单的多线程,需考虑线程安全问题。
- 销毁对话框时,确保线程能正常退出,避免资源泄露。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 08:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表