1. 函数说明
1.1 GetTempFileName
创建一个临时文件,并返回临时文件的名字
【1】 lpPathName 路径名
【2】lpPrefixString 前缀
【3】uUnique 是否唯一,0为唯一,否则不是。当设置为0时,会创建一个空文件,否则只是返回一个文件名
【4】 返回的文件名,格式为 <path>\ <pre><uuuu>.TMP
UINT WINAPI GetTempFileName( _In_ LPCTSTR lpPathName, _In_ LPCTSTR lpPrefixString, _In_ UINT uUnique, _Out_ LPTSTR lpTempFileName );
2. 代码
/* * FileName: SystemTempFile.cpp * Author: JarvisChu * Date: 2012-11-14 */ #include <stdio.h> #include <windows.h> #include <tchar.h> //switchs for the program //notice: do not open more than one //#define TEMP_FILE //#define PATH //#define FILE_TIME #define FILE_ATTRIBUTE #define BUFF_SIZE 256 int main() { //-------------------------------------------------------- #ifdef TEMP_FILE printf("--------Temp File----------------\n"); DWORD nLen; TCHAR szFileName[BUFF_SIZE]; TCHAR szLongPathBuff[BUFF_SIZE]; TCHAR szShortPathBuff[BUFF_SIZE]; GetTempFileName(_T("E:/Programming"), //the directory to create the temp file. '.' represents the current directory _T("jc"), //the prefix string of the temp file 0, szFileName ); //the szFileName can be string like "./jc258.tmp" or "./jc454.tmp" //and the file named "jc258.tmp" or "jc454.tmp" have been created in the current directory printf(_T("TempFile Name: %s\n"),szFileName); GetFullPathName(szFileName,BUFF_SIZE,szLongPathBuff,NULL); printf("FullPathName: %s\n",szLongPathBuff); // E:/Programming\jc5EC.tmp nLen = lstrlen(szLongPathBuff); GetShortPathName(szLongPathBuff,szShortPathBuff,nLen); printf("ShortPathName: %s\n",szShortPathBuff);// E:\PROGRA~2\jc5EC.tmp #endif //-------------------------------------------------------- #ifdef FILE_TIME printf("--------File Time----------------\n"); //GetFileTime() HANDLE hFile; FILETIME ftCreate,ftAccess,ftWrite,ftLocal; SYSTEMTIME stCreate; hFile = CreateFile(_T("C:/jarvischu.txt"),GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); GetFileTime(hFile,&ftCreate,&ftAccess,&ftWrite); // Convert the last-write time to local time. FileTimeToLocalFileTime(&ftWrite, &ftLocal); // Convert the local file time from UTC to system time. FileTimeToSystemTime(&ftLocal, &stCreate); HANDLE hConsoleOut = CreateFile("CONOUT$",GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); TCHAR szBuf[BUFF_SIZE]; DWORD nLen,nWrite; // Build a string showing the date and time. wsprintf(szBuf,"%02d/%02d/%d %02d:%02d\n", stCreate.wDay, stCreate.wMonth, stCreate.wYear, stCreate.wHour, stCreate.wMinute); nLen = lstrlen(szBuf); WriteConsole(hConsoleOut,szBuf,nLen,&nWrite,NULL); #endif //-------------------------------------------------------- #ifdef PATH printf("--------FullPath & ShortPath----------------\n"); DWORD nLen; TCHAR szLongPathBuff[BUFF_SIZE]; TCHAR szShortPathBuff[BUFF_SIZE]; GetFullPathName(_T("E:/Programming/jarvischu.txt"),BUFF_SIZE,szLongPathBuff,NULL); printf("FullPathName: %s\n",szLongPathBuff); //E:\Programming\jarvischu.txt nLen = lstrlen(szLongPathBuff); GetShortPathName(szLongPathBuff,szShortPathBuff,nLen); printf("ShortPathName: %s\n",szShortPathBuff);//E:\PROGRA~2\JARVIS~1.TXT #endif //-------------------------------------------------------- #ifdef FILE_ATTRIBUTE printf("--------File Attribute----------------\n"); DWORD dwAttr; dwAttr = GetFileAttributes("E:/Programming/jarvischu.rar"); if(dwAttr != -1) // -1 stands for file_not_exists or no rights to access the file { if( dwAttr & FILE_ATTRIBUTE_ARCHIVE) { printf("File is Archived!\n"); } if (dwAttr & FILE_ATTRIBUTE_NORMAL) { printf("File is Normal!\n"); } } #endif return 0; }