1. 程式人生 > >檔案操作記錄

檔案操作記錄

方法一。
這種方式無法監視到檔案的修改,只有右鍵屬性中修改檔案的操作才能監視到。

註冊

BOOL Register(HWND hWnd)

{



	SHChangeNotifyEntry  pfsne;

	LPITEMIDLIST   pidl;

	if(NOERROR == SHGetSpecialFolderLocation(hWnd,CSIDL_DESKTOP,&pidl))

		pfsne.pidl=pidl;

	pfsne.fRecursive = TRUE;

	 if(m_dwSHNotify == 0)

	 {

      m_dwSHNotify = SHChangeNotifyRegister(hWnd,

										  SHCNE_DISKEVENTS, 

										  SHCNE_ALLEVENTS, 

										   WM_FILEMONITOR, 

										   1, 

										   &pfsne);

	 }

	return TRUE;

}



反註冊

BOOL Unregister()

{

    SHChangeNotifyDeregister(m_dwSHNotify);

	m_dwSHNotify = 0;

	return TRUE;

}



自定義訊息

#define WM_FILEMONITOR  WM_USER+100



資料結構定義

typedef   struct  

{   

	DWORD    dwItem1;   

	DWORD    dwItem2;   

}SHNOTIFYSTRUCT;



標頭檔案中

afx_msg LONG  OnFileMonitor(WPARAM wParam,LPARAM lParam);



cpp檔案中

BEGIN_MESSAGE_MAP(CXXXDlg, CDialog)

	ON_MESSAGE(WM_FILEMONITOR, OnFileMonitor) //新增

END_MESSAGE_MAP()



//檔案監控 ,自定義訊息的處理函式

LONG  CXXDlg::OnFileMonitor(WPARAM wParam,LPARAM lParam)

{

	SHNOTIFYSTRUCT *pNotify = (SHNOTIFYSTRUCT *)wParam;   

	TCHAR szFileName[MAX_PATH*10], szNewFileName[MAX_PATH*10];   

	TCHAR szPath[MAX_PATH],szNewPath[MAX_PATH];   

	SHFILEINFO shFileInfo;   

	memset(szFileName,0,MAX_PATH*10);   

	memset(szPath,0,MAX_PATH);  

	memset(szNewFileName,0,MAX_PATH*10);

	memset(szNewPath,0,MAX_PATH);   



	if(pNotify->dwItem1)   

	{   

		SHGetFileInfo((TCHAR*)pNotify->dwItem1,0,&shFileInfo,sizeof(SHFILEINFO),SHGFI_PIDL | SHGFI_DISPLAYNAME);   

		SHGetPathFromIDList((struct _ITEMIDLIST *)pNotify->dwItem1,szPath);   

		wsprintf(szFileName,_T("Path:%s Name:%s "), szPath,shFileInfo.szDisplayName);  

		//	MessageBox(szFileName);

	} 



	if(pNotify->dwItem2)   

	{   

		SHGetFileInfo((TCHAR*)pNotify->dwItem2,0,&shFileInfo,sizeof(SHFILEINFO),SHGFI_PIDL   |   SHGFI_DISPLAYNAME);   

		SHGetPathFromIDList((struct _ITEMIDLIST *)pNotify->dwItem2,szNewPath);   //LPCITEMIDLIST

		wsprintf(szNewFileName,_T("Path:%s Name:%s "), szNewPath,shFileInfo.szDisplayName);   

		//MessageBox(szNewFileName);

	}   



	switch(lParam)   

	{   

	case   SHCNE_RENAMEITEM: //改名  

		break;



	case SHCNE_CREATE: //建立

		break;



	case SHCNE_DELETE:  //刪除

		break;



	case SHCNE_MKDIR :  //建立目錄

		break;



	case SHCNE_RMDIR :  //移除目錄

		break;



	case SHCNE_RENAMEFOLDER:

		break;



	case SHCNE_ATTRIBUTES : //改變

		break;

	}



	return 0;

}
//方法二
ReadDirectoryChangesW
用多執行緒監視,如果是監視U盤,導致U盤無法拔下