MakeSureDirectoryPathExists检查目录是否存在
来源:互联网 发布:成本测算软件 编辑:程序博客网 时间:2024/05/29 15:44
BOOL MakeSureDirectoryPathExists(
PCSTR Dirpath
);
函数作用
该函数的作用是检查指定目录是否存在,如果不存在则创建整个Dirpath所表示的整个目录。
参数
Dirpath:要检查的目录名。如果是路径不是文件名,需以 '\' 结尾。
返回值
如果目录存在,返回TRUE;如果不存在但全部路径创建成功,返回TRUE;
如果不存在且创建失败,返回FALSE。
一次性建立多级目录(用CreateDirectory只能一级一级的建立)。
如:
MakeSureDirectoryPathExists( "c:\\a\\b\\ ");
如果a文件夹不存在也可以创建成功。
这个函数并不存在于 Kernel32.dll 中,需要包含头文件imagehlp.h,并链接imagehlp.lib。
#include <ImageHlp.h> #pragma comment(lib,"imagehlp.lib")
#include <ImageHlp.h>
该函数的实现如下,以供使用和学习。
LPTSTR _tCharAlloc(UINT uSize){return (LPTSTR)malloc(sizeof(TCHAR) * uSize);}VOID _tCharFree(LPVOID p){free(p);}#define IMAGEAPI WINAPIBOOL IMAGEAPI MakeSureDirectoryPathExists(LPCTSTR pszDirPath){LPTSTR p, pszDirCopy;DWORD dwAttributes;// Make a copy of the string for editing.__try{pszDirCopy = (LPTSTR)_tCharAlloc(lstrlen(pszDirPath) + 1);if(pszDirCopy == NULL)return FALSE;lstrcpy(pszDirCopy, pszDirPath);p = pszDirCopy;// If the second character in the path is "\", then this is a UNC// path, and we should skip forward until we reach the 2nd \ in the path.if((*p == TEXT('\\')) && (*(p+1) == TEXT('\\'))){p++; // Skip over the first \ in the name.p++; // Skip over the second \ in the name.// Skip until we hit the first "\" (\\Server\).while(*p && *p != TEXT('\\')){p = CharNext(p);}// Advance over it.if(*p){p++;}// Skip until we hit the second "\" (\\Server\Share\).while(*p && *p != TEXT('\\')){p = CharNext(p);}// Advance over it also.if(*p){p++;}}else if(*(p+1) == TEXT(':')) // Not a UNC. See if it's <drive>:{p++;p++;// If it exists, skip over the root specifierif(*p && (*p == TEXT('\\'))){p++;}}while(*p){if(*p == TEXT('\\')){*p = TEXT('\0');dwAttributes = GetFileAttributes(pszDirCopy);// Nothing exists with this name. Try to make the directory name and error if unable to.if(dwAttributes == 0xffffffff){if(!CreateDirectory(pszDirCopy, NULL)){if(GetLastError() != ERROR_ALREADY_EXISTS){_tCharFree(pszDirCopy);return FALSE;}}}else{if((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY){// Something exists with this name, but it's not a directory... Error_tCharFree(pszDirCopy);return FALSE;}}*p = TEXT('\\');}p = CharNext(p);}}__except(EXCEPTION_EXECUTE_HANDLER){// SetLastError(GetExceptionCode());_tCharFree(pszDirCopy);return FALSE;}_tCharFree(pszDirCopy);return TRUE;}
0 0
- MakeSureDirectoryPathExists检查目录是否存在
- 2014-03-14周五工作日志:检查指定目录是否存在的函数MakeSureDirectoryPathExists
- 检查目录是否存在
- nodejs-检查目录文件是否存在
- node.js中检查目录是否存在
- 检查目录是否存在,不存在则创建之
- 给定文件列表,检查目录下的文件是否存在
- 检查指定目录是否存在,不存在直接创建
- VB.NET 判断文件夹目录与文件是否存在检查
- 检查某个文件或目录是否存在的函数
- LinuxShell-检查指定目录是否存在shell脚本文件
- 检查文件是否存在
- 检查网页是否存在
- 检查进程是否存在
- 检查用户名是否存在
- 检查数据库是否存在
- 检查数据链是否存在
- 检查变量是否存在
- C++通过域名得到IP
- 马小妹的java之旅——注解Annotation
- Oil Deposits
- idea maven 部署服务器tomcat
- C语言,将一个数组中的值按逆序重新存放,例如,原来的顺序是8,6,5,4,1。要求改为1,4,5,6,8
- MakeSureDirectoryPathExists检查目录是否存在
- java多线程实例
- Cg语言 SV_POSITION vs POSITION
- Handler 非静态内部引起的内存泄漏问题
- EJB到底是什么,真的那么神秘吗??
- spring websocket让页面实时获取数据
- Java虚拟机----自定义类加载器:实现对字节码的加密解密
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 12 java内存模型与线程