Traversing disk capacity:
#include <> #include <> void GetDrivesType(const char* lpRootPathName) { UINT uDriverType = GetDriveType(lpRootPathName); switch (uDriverType) { case DRIVE_UNKNOWN:puts("Unknown Disk"); break; case DRIVE_NO_ROOT_DIR: puts("Invalid path"); break; case DRIVE_REMOVABLE: puts("Removable Disk"); break; case DRIVE_FIXED: puts("Fixed Disk"); break; case DRIVE_REMOTE: puts("Network Disk"); break; case DRIVE_CDROM: puts("Optical Drive"); break; case DRIVE_RAMDISK: puts("Memory Mapping Disk"); break; default: break; } } void GetDrivesFreeSpace(const char* lpRootPathName) { unsigned long long available, total, free; if (GetDiskFreeSpaceEx(lpRootPathName, (ULARGE_INTEGER*)&available, (ULARGE_INTEGER*)&total, (ULARGE_INTEGER*)&free)) { printf("Disk: %s | Total: %lld MB Used: %lld MB Remaining: %lld MB \n", lpRootPathName, total >> 20, available >> 20, free >> 20); } } int main(int argc,char *argv[]) { DWORD dwSize = MAX_PATH; char szLogicalDrives[MAX_PATH] = {0}; // Get the logical drive letter string DWORD dwResult = GetLogicalDriveStringsA(dwSize, szLogicalDrives); if (dwResult > 0 && dwResult <= MAX_PATH) { char* szSingleDrive = szLogicalDrives; // Start from the buffer start address while (*szSingleDrive) { //printf("Drive: %s\n", szSingleDrive); // Output the drive letter of a single drive // GetDrivesType(szSingleDrive); GetDrivesFreeSpace(szSingleDrive); szSingleDrive += strlen(szSingleDrive) + 1; // Get the next drive address } } system("pause"); return 0; }
Traversing file-specific paths:
Loop through the file path and filter out the path with the file suffixed .exe.
#include <> #include <> #include <> void SearchFile(char *pszDirectory) { // Search for file of specified type char *pszFileName = NULL; char *pTempSrc = NULL; WIN32_FIND_DATA FileData = { 0 }; // Apply for dynamic memory pszFileName = new char[2048]; pTempSrc = new char[2048]; // Construct the search file type string *.* to indicate searching for all file types wsprintf(pszFileName, "%s\\*.*", pszDirectory); HANDLE hFile = ::FindFirstFile(pszFileName, &FileData); if (INVALID_HANDLE_VALUE != hFile) { do { // Filter out the current directory "." and the previous directory ".." if ('.' == [0]) continue; // Splice file path wsprintf(pTempSrc, "%s\\%s", pszDirectory, ); // Determine whether it is a directory or a file if ( & FILE_ATTRIBUTE_DIRECTORY) SearchFile(pTempSrc); // If it is a directory, continue to recurse else { char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT]; _splitpath(pTempSrc, drive, dir, fname, ext); // If it is a file and the suffix is .exe, output the specific path if (strcmp(ext, ".exe") == 0) printf("%s \n", pTempSrc); } } while (::FindNextFile(hFile, &FileData)); } FindClose(hFile); delete[]pTempSrc; delete[]pszFileName; } int main(int argc, char * argv[]) { SearchFile("c:\\MinGW7"); system("pause"); return 0; }
Monitor file directory changes:
#include <> #include <> #include <> UINT MonitorFileThreadProc(LPVOID lpVoid) { char *pszDirectory = (char *)lpVoid; // Open the directory and get the file handle HANDLE hDirectory = CreateFile(pszDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (INVALID_HANDLE_VALUE == hDirectory) return 1; char szFileName[MAX_PATH] = { 0 }; BOOL bRet = FALSE; DWORD dwRet = 0; DWORD dwBufferSize = 2048; // Apply for a large enough buffer BYTE *pBuf = new BYTE[dwBufferSize]; if (NULL == pBuf) return 2; FILE_NOTIFY_INFORMATION *pFileNotifyInfo = (FILE_NOTIFY_INFORMATION *)pBuf; // Start cyclic setting monitoring do { RtlZeroMemory(pFileNotifyInfo, dwBufferSize); // Set up the monitoring directory bRet = ReadDirectoryChangesW(hDirectory, pFileNotifyInfo, dwBufferSize, TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | // Modify the file name FILE_NOTIFY_CHANGE_ATTRIBUTES | // Modify file properties FILE_NOTIFY_CHANGE_LAST_WRITE, // Last write &dwRet, NULL, NULL); if (FALSE == bRet) break; // Convert wide characters into narrow characters, wide byte string to multi-byte string WideCharToMultiByte(CP_ACP, 0, (wchar_t *)(&pFileNotifyInfo->FileName), (pFileNotifyInfo->FileNameLength / 2),szFileName,MAX_PATH,NULL,NULL); // Connect the path to the file into a complete file path char FullFilePath[1024] = { 0 }; strncpy(FullFilePath, pszDirectory, strlen(pszDirectory)); strcat(FullFilePath, szFileName); // Determine the operation type and display it switch (pFileNotifyInfo->Action) { case FILE_ACTION_ADDED: printf("The file was [created]: %s \n", FullFilePath); break; case FILE_ACTION_REMOVED: printf("File is [deleted]: %s \n", FullFilePath); break; case FILE_ACTION_MODIFIED: printf("The file was [modified]: %s \n", FullFilePath); break; case FILE_ACTION_RENAMED_OLD_NAME: printf("The file was [renamed]: %s \n", FullFilePath); break; } } while (bRet); CloseHandle(hDirectory); delete[] pBuf; pBuf = NULL; return 0; } int main(int argc, char * argv[]) { char *pszDirectory = "C:\\"; // Create thread to start monitoring CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MonitorFileThreadProc, pszDirectory, 0, NULL); while (1) { Sleep(10000); } system("pause"); return 0; }
Monitor directory file changes:
It can be changed to a simple file tamper-proof program, or used to monitor virus behavior.
#include <> #include <> #include <> DWORD WINAPI MonitorFileThreadProc(LPVOID lParam) { char *pszDirectory = (char *)lParam; BOOL bRet = FALSE; BYTE Buffer[1024] = { 0 }; FILE_NOTIFY_INFORMATION *pBuffer = (FILE_NOTIFY_INFORMATION *)Buffer; DWORD dwByteReturn = 0; HANDLE hFile = CreateFile(pszDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (INVALID_HANDLE_VALUE == hFile) return 1; while (TRUE) { ZeroMemory(Buffer, sizeof(Buffer)); // Set the monitoring directory callback function bRet = ReadDirectoryChangesW(hFile,&Buffer,sizeof(Buffer),TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | // Modify the file name FILE_NOTIFY_CHANGE_ATTRIBUTES | // Modify file properties FILE_NOTIFY_CHANGE_LAST_WRITE, // Last write &dwByteReturn, NULL, NULL); if (TRUE == bRet) { char szFileName[MAX_PATH] = { 0 }; // Convert wide characters into narrow characters, wide byte string to multi-byte string WideCharToMultiByte(CP_ACP,0,pBuffer->FileName,(pBuffer->FileNameLength / 2), szFileName,MAX_PATH,NULL,NULL); // Connect the path to the file into a complete file path char FullFilePath[1024] = { 0 }; strncpy(FullFilePath, pszDirectory, strlen(pszDirectory)); strcat(FullFilePath, szFileName); switch (pBuffer->Action) { case FILE_ACTION_ADDED: { printf("Add: %s \n", FullFilePath); break; } case FILE_ACTION_REMOVED: { printf("Delete: %s \n", FullFilePath); break; } case FILE_ACTION_MODIFIED: { printf("Modify: %s \n", FullFilePath); break; } case FILE_ACTION_RENAMED_OLD_NAME: { printf("Rename: %s", szFileName); if (0 != pBuffer->NextEntryOffset) { FILE_NOTIFY_INFORMATION *tmpBuffer = (FILE_NOTIFY_INFORMATION *) ((DWORD)pBuffer + pBuffer->NextEntryOffset); switch (tmpBuffer->Action) { case FILE_ACTION_RENAMED_NEW_NAME: { ZeroMemory(szFileName, MAX_PATH); WideCharToMultiByte(CP_ACP,0,tmpBuffer->FileName, (tmpBuffer->FileNameLength / 2), szFileName,MAX_PATH,NULL,NULL); printf(" -> %s \n", szFileName); break; } } } break; } case FILE_ACTION_RENAMED_NEW_NAME: { printf("Rename(new): %s \n", FullFilePath); break; } } } } CloseHandle(hFile); return 0; } int main(int argc, char * argv[]) { char *pszDirectory = "C:\\"; HANDLE hThread = CreateThread(NULL, 0, MonitorFileThreadProc, pszDirectory, 0, NULL); WaitForSingleObject(hThread, INFINITE); CloseHandle(hThread); return 0; }
The above is the detailed content of the examples of C/C++ monitoring disk and directory operations. For more information about C/C++ monitoring disk and directory operations, please pay attention to my other related articles!
Article author: lyshark
Source of the article:/lyshark