监视文件路径

来源:互联网 发布:mac已导入照片怎么删除 编辑:程序博客网 时间:2024/05/16 12:59

根据选择监视指定文件夹, 由于没有找到CVI是否有相关封装函数,因此调用的是WINDOWS API函数进行监视,简单界面设计如图:

STRING 为 路径显示控件,TEXTMSG_2为结果显示的第一行,TEXTMSG_3为结果显示的第二行。



#include <ansi_c.h>#include "windows.h"#include <utility.h>#include <stdio.h>#include <cvirte.h>#include <userint.h>#include "monitor.h"static int panelHandle;static int run=0;void monitorFile(HANDLE handle,char resultData[][1024]);void delay(double time);int main (int argc, char *argv[]){if (InitCVIRTE (0, argv, 0) == 0)return -1;/* out of memory */if ((panelHandle = LoadPanel (0, "monitor.uir", PANEL)) < 0)return -1;DisplayPanel (panelHandle);RunUserInterface ();DiscardPanel (panelHandle);return 0;}int CVICALLBACK panelCB (int panel, int event, void *callbackData, int eventData1, int eventData2){switch (event){case EVENT_CLOSE:QuitUserInterface (0);break;case EVENT_PANEL_MINIMIZE:break;}return 0;}int CVICALLBACK monitorDir (int panel, int control, int event,void *callbackData, int eventData1, int eventData2){char path[500]={0};switch (event){case EVENT_COMMIT:DirSelectPopup ("c:\\Users\\Administrator\\Desktop", "Select Directory", 1, 1, path);SetCtrlVal(panelHandle,PANEL_STRING,path);break;}return 0;}int CVICALLBACK quit (int panel, int control, int event,  void *callbackData, int eventData1, int eventData2){switch (event){case EVENT_COMMIT:run=0;break;}return 0;}int CVICALLBACK monitor (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){char path[500];char resultData[2][1024]={0};switch (event){case EVENT_COMMIT:GetCtrlVal(panelHandle,PANEL_STRING,path);run=1;HANDLE handle = CreateFileA((LPCWSTR)path,  FILE_LIST_DIRECTORY,   FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE,NULL,  OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,  NULL);monitorFile(handle,resultData);SetCtrlVal (panelHandle, PANEL_TEXTMSG_2, resultData[0]);SetCtrlVal (panelHandle, PANEL_TEXTMSG_3, resultData[1]);break;}return 0;}/*--------------------------------------------------------------------------- *  实现功能:监控 *-------------------------------------------------------------------------*/void monitorFile(HANDLE handle,char resultData[][1024]){char notify[1024] = {0};FILE_NOTIFY_INFORMATION *pnotify=(FILE_NOTIFY_INFORMATION *)notify;DWORD cbBytes;//--------------------------------------------------------------------------------------------------------------while(run == 1){BOOL result = ReadDirectoryChangesW(handle, ¬ify, sizeof(notify), TRUE,FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_CREATION, &cbBytes, NULL, NULL);if(result){if(pnotify->Action == FILE_ACTION_ADDED)strcpy(resultData[1],"add new file");else if(pnotify->Action == FILE_ACTION_REMOVED)strcpy(resultData[1],"delete file");else if(pnotify->Action == FILE_ACTION_MODIFIED)strcpy(resultData[1],"change file");WideCharToMultiByte(CP_ACP,0,pnotify->FileName,pnotify->FileNameLength/2,resultData[0],1024,NULL,NULL);if((pnotify->NextEntryOffset != 0) && (pnotify->FileNameLength) > 0 && (pnotify->FileNameLength < MAX_PATH)){PFILE_NOTIFY_INFORMATION p = (PFILE_NOTIFY_INFORMATION)((char*)pnotify+pnotify->NextEntryOffset);   WideCharToMultiByte(CP_ACP,0,p->FileName,p->FileNameLength/2,resultData[0],1024,NULL,NULL);strcpy(resultData[1],"change file name");}return ;}delay(1);}//-------------------------------------------------------------------------------------------------------------------}void delay(double time){for(int i=0;i<time*100;i++){Delay(0.01);ProcessDrawEvents();ProcessSystemEvents();}}

0 0
原创粉丝点击