1 /* 2 获取系统目录,并保存在文件里 3 [peoject->set->link->project chose->subsystem:console] 4 */ 5 #include<windows.h> 6 int main(int argc,TCHAR argv[]) 7 { 8 //文件句柄 9 HANDLE hFile; 10 DWORD dwWritten; 11 //字符数组,用于存储系统目录 12 TCHAR szSystemDir[MAX_PATH]; 13 //获取系统目录 14 GetSystemDirectory(szSystemDir,MAX_PATH); 15 16 //创建文件 17 hFile=CreateFile("systemroot.txt", 18 GENERIC_WRITE, 19 0,NULL,CREATE_ALWAYS, 20 FILE_ATTRIBUTE_NORMAL, 21 NULL); 22 //判断文件是否创建成功 23 if(hFile!= INVALID_HANDLE_VALUE) 24 { 25 //write systemRoot into file 26 if(!WriteFile(hFile,szSystemDir,lstrlen(szSystemDir),&dwWritten,NULL)) 27 { 28 return GetLastError(); 29 } 30 } 31 //close file and return 32 CloseHandle(hFile); 33 return 0; 34 }