- 拷贝文件
1 //拷贝文件 2 void copy() 3 { 4 //自身路径 5 char selfpath[256] = { 0 }; 6 //Windows路径 7 char windowpath[256] = { 0 }; 8 //系统路径 9 char systempath[256] = { 0 }; 10 11 //获取自身路径 12 GetModuleFileNameA(NULL, selfpath, 256); 13 //获取Windows路径 14 GetWindowsDirectoryA(windowpath, 256); 15 //获取系统路径 16 GetSystemDirectoryA(systempath, 256); 17 strcat(windowpath, "\\test.exe"); 18 strcat(systempath, "\\test.exe"); 19 //输出目录 20 printf("%s\n", selfpath); 21 printf("%s\n", windowpath); 22 printf("%s\n", systempath); 23 //拷贝函数 24 CopyFileA(selfpath, windowpath, FALSE); 25 }
- 遍历所有磁盘信息
1 //遍历所有磁盘信息 2 void find_all() 3 { 4 char szbuf[100] = { 0 }; 5 //载入磁盘信息 6 GetLogicalDriveStrings(100, szbuf); 7 char *p = szbuf; 8 while (*p != ‘\0‘) 9 { 10 printf("%s\n", p); 11 getspace(p); 12 p += strlen(p) + 1; 13 } 14 }
- 获取磁盘大小
1 //获取磁盘大小 2 int getspace(char *pstr) 3 { 4 //剩余空间 可用空间 总大小 5 ULARGE_INTEGER dwl, dwfree, dwtol; 6 char szsize[128] = { 0 }; 7 double size = 0; 8 GetDiskFreeSpaceEx(pstr, &dwl, &dwtol, &dwfree); 9 size = dwtol.QuadPart / 1024.0 / 1024 / 1204;//GB 10 printf("总大小:%f GB\n", size); 11 size = dwl.QuadPart / 1024.0 / 1024 / 1204;//GB 12 printf("剩余空间:%f GB\n", size); 13 size = dwfree.QuadPart / 1024.0 / 1024 / 1204;//GB 14 printf("可用空间:%f GB\n", size); 15 }
完整代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <Windows.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 //获取磁盘大小 6 int getspace(char *pstr) 7 { 8 //剩余空间 可用空间 总大小 9 ULARGE_INTEGER dwl, dwfree, dwtol; 10 char szsize[128] = { 0 }; 11 double size = 0; 12 GetDiskFreeSpaceEx(pstr, &dwl, &dwtol, &dwfree); 13 size = dwtol.QuadPart / 1024.0 / 1024 / 1204;//GB 14 printf("总大小:%f GB\n", size); 15 size = dwl.QuadPart / 1024.0 / 1024 / 1204;//GB 16 printf("剩余空间:%f GB\n", size); 17 size = dwfree.QuadPart / 1024.0 / 1024 / 1204;//GB 18 printf("可用空间:%f GB\n", size); 19 } 20 21 //遍历所有磁盘信息 22 void find_all() 23 { 24 char szbuf[100] = { 0 }; 25 //载入磁盘信息 26 GetLogicalDriveStrings(100, szbuf); 27 char *p = szbuf; 28 while (*p != ‘\0‘) 29 { 30 printf("%s\n", p); 31 getspace(p); 32 p += strlen(p) + 1; 33 } 34 } 35 36 //拷贝文件 37 void copy() 38 { 39 //自身路径 40 char selfpath[256] = { 0 }; 41 //Windows路径 42 char windowpath[256] = { 0 }; 43 //系统路径 44 char systempath[256] = { 0 }; 45 46 //获取自身路径 47 GetModuleFileNameA(NULL, selfpath, 256); 48 //获取Windows路径 49 GetWindowsDirectoryA(windowpath, 256); 50 //获取系统路径 51 GetSystemDirectoryA(systempath, 256); 52 strcat(windowpath, "\\test.exe"); 53 strcat(systempath, "\\test.exe"); 54 //输出目录 55 printf("%s\n", selfpath); 56 printf("%s\n", windowpath); 57 printf("%s\n", systempath); 58 //拷贝函数 59 CopyFileA(selfpath, windowpath, FALSE); 60 } 61 62 void main() 63 { 64 find_all(); 65 system("pause"); 66 67 system("pause"); 68 }