在项目中用到一个检测共享文件夹下新增图像,获取新增图像进行图像分类,下面代码监测共享文件下新图像的添加,读取新增图像进行路径,进行或许对图像操作。
#include "stdafx.h"
#include "iostream"
#include
#include
#include
#include
#include "stdio.h"
#pragma comment(lib, "Mpr.lib")
using namespace std;
int _tmain(int argc, TCHAR **argv)
{
TCHAR szPasswd[] = TEXT("90523"); //共享资源授权用户的密码
TCHAR szUserName[] = TEXT("Administrator"); //共享资源授权的用户
NETRESOURCE net_Resource;
net_Resource.dwDisplayType = RESOURCEDISPLAYTYPE_DIRECTORY;
net_Resource.dwScope = RESOURCE_CONNECTED;
net_Resource.dwType = RESOURCETYPE_ANY;
net_Resource.dwUsage = 0;
net_Resource.lpComment = TEXT("");
net_Resource.lpLocalName = TEXT("z:"); //映射成本地驱动器z:
net_Resource.lpProvider = NULL;
net_Resource.lpRemoteName = TEXT("\\\\192.168.3.168\\testShare”); // \\servername\共享资源名
DWORD dwFlags = CONNECT_UPDATE_PROFILE;
WNetCancelConnection2(net_Resource.lpLocalName, CONNECT_UPDATE_PROFILE, TRUE);
return 1;
DWORD dw = WNetAddConnection2(&net_Resource, szPasswd, szUserName, dwFlags);
switch (dw)
{
case ERROR_SUCCESS:
ShellExecute(NULL, TEXT("open"), TEXT("z:"), NULL, NULL, SW_SHOWNORMAL);
break;
case ERROR_ACCESS_DENIED:
wprintf(TEXT("没有权访问!"));
break;
case ERROR_ALREADY_ASSIGNED:
ShellExecute(NULL, TEXT("open"), TEXT("z:"), NULL, NULL, SW_SHOWNORMAL);
break;
case ERROR_INVALID_ADDRESS:
wprintf(TEXT("IP地址无效"));
break;
case ERROR_NO_NETWORK:
wprintf(TEXT("网络不可达!"));
break;
}
fileWatcher();
return 0;
}
void fileWatcher()
{
DWORD cbBytes;
char file_name[MAX_PATH]; //设置文件名;
char file_rename[MAX_PATH]; //设置文件重命名后的名字;
char notify[1024];
int count = 0; //文件数量。可能同时拷贝、删除多个文件,可以进行更友好的提示;
TCHAR *dir = _T(“\\\\192.168.3.168\\testShare”);
HANDLE dirHandle = CreateFile(dir,
GENERIC_READ | GENERIC_WRITE | FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (dirHandle == INVALID_HANDLE_VALUE) //若网络重定向或目标文件系统不支持该操作,函数失败,同时调用GetLastError()返回ERROR_INVALID_FUNCTION
{
cout << "error" + GetLastError() << endl;
}
memset(notify, 0, strlen(notify));
FILE_NOTIFY_INFORMATION *pnotify = (FILE_NOTIFY_INFORMATION*)notify;
cout << "Start Monitor..." << endl;
while (true)
{
if (ReadDirectoryChangesW(dirHandle, ¬ify, 1024, true,
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE,
&cbBytes, NULL, NULL))
{
//转换文件名为多字节字符串;
if (pnotify->FileName)
{
memset(file_name, 0, strlen(file_name));
WideCharToMultiByte(CP_ACP, 0, pnotify->FileName, pnotify->FileNameLength / 2, file_name, 99, NULL, NULL);
}
//获取重命名的文件名;
if (pnotify->NextEntryOffset != 0 && (pnotify->FileNameLength > 0 && pnotify->FileNameLength < MAX_PATH))
{
PFILE_NOTIFY_INFORMATION p = (PFILE_NOTIFY_INFORMATION)((char*)pnotify + pnotify->NextEntryOffset);
memset(file_rename, 0, sizeof(file_rename));
WideCharToMultiByte(CP_ACP, 0, p->FileName, p->FileNameLength / 2, file_rename, 99, NULL, NULL);
}
//设置类型过滤器,监听文件创建、更改、删除、重命名等;
switch (pnotify->Action)
{
case FILE_ACTION_ADDED:
count++;
cout << "count = " << count << endl;
cout << setw(5) << "file add:" << setw(5) << file_name << endl;
break;
case FILE_ACTION_MODIFIED:
cout << "file modified:" << setw(5) << file_name << endl;
break;
case FILE_ACTION_REMOVED:
count++;
cout << count << setw(5) << "file removed:" << setw(5) << file_name << endl;
break;
case FILE_ACTION_RENAMED_OLD_NAME:
cout << "file renamed:" << setw(5) << file_name << "->" << file_rename << endl;
break;
default:
cout << "UNknow command!" << endl;
}
}
}
CloseHandle(dirHandle);
}