演示代码:
1 /*header file*/ 2 #include <Windows.h> 3 4 int main(int argc,TCHAR argv[]) 5 { 6 //file handle 7 HANDLE hFile; 8 DWORD dwWritten; 9 //char array ,store system dir 10 TCHAR szSystemDir[MAX_PATH]; 11 //get system dir 12 GetSystemDirectory(szSystemDir,MAX_PATH); 13 14 //make file systemroot.txt 15 hFile = CreateFile("systemroot.txt", 16 GENERIC_WRITE, 17 0,NULL,CREATE_ALWAYS, 18 FILE_ATTRIBUTE_NORMAL, 19 NULL); 20 //IF the create file 21 if(hFile != INVALID_HANDLE_VALUE) 22 { 23 //write the system dir info to file 24 if(!WriteFile(hFile,szSystemDir,lstrlen(szSystemDir),&dwWritten,NULL)) 25 { 26 return GetLastError(); 27 } 28 } 29 CloseHandle(hFile); 30 return 0; 31 32 }
运行效果: