7个主要函数。TempPath()获取临时文件目录;CurrentPath()获取当前工作目录;SystemPath()获取system32目录;WindowsPath()获取Windows目录;ShortPath()获取短文件名;ExePath()获取exe可执行文件目录;GetDir()获取指定文件的目录路径。
#include <stdio.h>
#include <windows.h>
/*将给定的两个字符串连接起来,并返回为一个新的字符串*/
char* StringJoin(const char* first,const char* last)
{
char* result;
int len = strlen(first)+strlen(last)+1;
result = (char*)malloc(len*sizeof(char));
memset(result,0,len*sizeof(char));
strcpy(result,first);
strcat(result,last);
return result;
}
/*用StringJoin函数,在字符串末尾添加'\',用于目录路径*/
char* add_slash(char* string)
{
int len = strlen(string);
/*查看最后一个字符是否为'\'*/
if(string[len-1] != '\\')
return StringJoin(string,"\\");
else
return string;
}
/*获取指定字符串指定部分*/
char* StringSub(const char* string,int start,int number)
{
int len = strlen(string);
char* temp;
if(start>len)
{
printf("Start %d is too big than string length %d!\n",start,len);
return NULL;
}
if(number>(len-start))
{
printf("Number is too big!\n");
number = len - start + 1;
}
temp = (char*)malloc(sizeof(char)*(number+1));
memset(temp,0,(number+1)*sizeof(char));
int i = 0;
int j = 0;
while(i != number)
temp[i++] = string[(start-1)+j++];
temp[number]='\0';
return temp;
}
/*获取临时目录*/
char* TempPath()
{
char *Buffer;
DWORD dwRet;
/*获取临时目录*/
/*获取临时目录字符串大小,包含末尾的'\0'*/
dwRet = GetTempPath(0,NULL);
if( dwRet == 0 )
{
printf("GetCurrentDirectory failed (%d)\n", GetLastError());
return NULL;
}
/*根据获取的空间,分配内存,并置零*/
Buffer = (char *)malloc(sizeof(char)*dwRet);
memset(Buffer,0,dwRet*sizeof(char));
/*获取临时目录*/
GetTempPath(dwRet,Buffer);
return add_slash(Buffer);
}
/*获取当前工作目录*/
char* CurrentPath()
{
char *Buffer;
DWORD dwRet;
/*获取当前目录*/
/*获取当前目录字符串大小,包含末尾的'\0'*/
dwRet = GetCurrentDirectoryA (0,NULL);
if( dwRet == 0 )
{
printf("GetCurrentDirectory failed (%d)\n", GetLastError());
return NULL;
}
/*根据获取的空间,分配内存,并置零*/
Buffer = (char *)malloc(sizeof(char)*dwRet);
memset(Buffer,0,dwRet*sizeof(char));
/*获取临时目录*/
GetCurrentDirectoryA (dwRet,Buffer);
return add_slash(Buffer);
}
/*获取系统System32目录*/
char* SystemPath()
{
char *Buffer;
DWORD dwRet;
/*获取System目录*/
/*获取System目录字符串大小,包含末尾的'\0'*/
dwRet = GetSystemDirectoryA (NULL,0);
if( dwRet == 0 )
{
printf("GetCurrentDirectory failed (%d)\n", GetLastError());
return NULL;
}
/*根据获取的空间,分配内存,并置零*/
Buffer = (char *)malloc(sizeof(char)*dwRet);
memset(Buffer,0,dwRet*sizeof(char));
/*获取临时目录*/
GetSystemDirectoryA (Buffer,dwRet);
return add_slash(Buffer);
}
/*获取Windows目录*/
char* WindowsPath()
{
char *Buffer;
DWORD dwRet;
/*获取Windows目录*/
/*获取Windows目录字符串大小,包含末尾的'\0'*/
dwRet = GetWindowsDirectoryA (NULL,0);
if( dwRet == 0 )
{
printf("GetCurrentDirectory failed (%d)\n", GetLastError());
return NULL;
}
/*根据获取的空间,分配内存,并置零*/
Buffer = (char *)malloc(sizeof(char)*dwRet);
memset(Buffer,0,dwRet*sizeof(char));
/*获取临时目录*/
GetWindowsDirectoryA (Buffer,dwRet);
return add_slash(Buffer);
}
/*获取一个文件的短文件名*/
char* ShortPath(char* Path)
{
char *Buffer;
DWORD dwRet;
dwRet = GetShortPathNameA(Path,NULL,0);
if(dwRet == 0)
return NULL;
Buffer = (char*)malloc(sizeof(char)*dwRet);
memset(Buffer,0,dwRet*sizeof(char));
GetShortPathNameA(Path,Buffer,dwRet);
return Buffer;
}
/*获取当前运行程序的目录*/
char* ExePath()
{
char *Buffer;
/**/
DWORD size=32;
DWORD dwRet=32;
Buffer = (char*)malloc(sizeof(char)*dwRet);
memset(Buffer,0,dwRet*sizeof(char));
/*获取Windows目录*/
/*获取Windows目录字符串大小,包含末尾的'\0'*/
dwRet = GetModuleFileNameA(NULL,Buffer,dwRet);
if(dwRet == 0)
return NULL;
while (size == dwRet)
{
size *= 2;
Buffer = (char*)realloc(Buffer,sizeof(char)*size);
memset(Buffer,0,size*sizeof(char));
dwRet = GetModuleFileNameA(NULL,Buffer,size);
}
return Buffer;
}
/*获取一个指定文件的目录*/
char* GetDir(const char* filepath)
{
char* pos = strrchr(filepath,'\\');
if(pos == NULL)
return NULL;
else
return StringSub(filepath,1,pos + 1 - filepath);
}
int main()
{
printf("%s\n",TempPath());
printf("%s\n",CurrentPath());
printf("%s\n",SystemPath());
printf("%s\n",WindowsPath());
printf("%s\n",ShortPath("C:\\Program Files\\7-Zip\\readme.txt"));
printf("%s\n",ExePath());
printf("%s\n",GetDir("C:\\aaa\\bbb.exe"));
printf("%s\n",add_slash("C:\\Windows\\"));
printf("%s\n",add_slash("C:\\Windows"));
return 0;
}