博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VC中判断目录是否存在
阅读量:2392 次
发布时间:2019-05-10

本文共 1829 字,大约阅读时间需要 6 分钟。

方法一:

CFileFind m_FileFind;

CString m_sFilePath = "D://Program Files//Adobe//Adobe Flash CS3";
if(!m_FileFind.FindFile(m_sFilePath))  //路径不存在则创建该路径
{
      CreateDirectory(m_sFilePath,NULL);
}

 

 

方法二:

 CString   strFilePath;

strFilePath   =   "E://图像//";
strFilePath   +=   m_ID;
if   (!SetCurrentDirectory(strFilePath))   
{
     CreateDirectory(strFilePath, NULL);
}

 

方法三:

FindFirstFile()/

#define _WIN32_WINNT 0x0400
#include "windows.h"
int
main(int argc, char *argv[])
{
  WIN32_FIND_DATA FindFileData;
  HANDLE hFind;
  printf ("Target file is %s./n", argv[1]);
  hFind = FindFirstFile(argv[1], &FindFileData);
  if (hFind == INVALID_HANDLE_VALUE) {
    printf ("Invalid File Handle. Get Last Error reports %d/n", GetLastError ());
  } else {
    printf ("The first file found is %s/n", FindFileData.cFileName);
    FindClose(hFind);
  }
  return (0);
}

 

方法四:

FILE *fp = fopen(strPath, "r")

if(fp == NULL)
{
//没有同名文件
}

 

 

方法五:

  CString   strPath;  
  strPath   =   "c://abc.dat"  
  if(GetFileAttributes(strPath)   ==   0xFFFFFFFF)  
  {  
  AfxMessageBox("文件不存在");  
  return;  
  }  

 

程序如下:  

  CString   filePath   =   "C://abc//";  
  DWORD   dwAttr   =   GetFileAttributes(filePath);  
  if(dwAttr==-1||(dwAttr&FILE_ATTRIBUTE_DIRECTORY)==0)//目录不存在  
  {  
  if   (   !CreateDirectory(filePath,   NULL)   )  
  {  
  AfxMessageBox("不能创建目录");  
  return;  
  }  
  }  

 

方法六:

  WIN32_FIND_DATA   tFile;  

  memset(&tFile,   0,   sizeof(tFile));  
  CString   str   =   "d:/eeagadg/...";  
  if(FindFirstFile(str,   &tFile)   ==   INVALID_HANDLE_..)  
          不存在
 

方法七:

  int   _access(    

        const   char   *path,    
        int   mode    
  );  

  #include   <io.h>  

  .....  
  char   szPathName[MAX_PATH]   =   "C://ABC";  
  if(_access(szPathName,0)   ==   0)  
        MessageBox(NULL,"该文件夹已经存在","提示",0)  
  else  
    CreateDirectory(szPathName,NULL);

---------------------------------------------------------------------------------------

我在MFC程序中 用的是方法一:

 

 CString file;

 CFileFind fileFind;

 

 file=_T("D://T1//T2");

 

   if (!fileFind.FindFile(file))

   {
   CreateDirectory(file,NULL);
   }

 

因为用其他方法时,可能要加额外的头文件 所以选择了这一个

 

转载地址:http://jweab.baihongyu.com/

你可能感兴趣的文章
开源搜索技术的核心引擎 —— Lucene
查看>>
万亿级图数据库 Nebula Graph 的数据模型和架构设计
查看>>
码洞技术文章大全
查看>>
一个Raft开源项目的结构分析
查看>>
RPC 服务器之【多进程描述符传递】高阶模型
查看>>
程序员年龄增大后的职业出路是什么?
查看>>
快学 Go 语言 第 1 课 —— Hello World
查看>>
《快学 Go 语言》第 4 课 —— 低调的数组
查看>>
作为程序员,你是如何在工作以后找到女朋友的?
查看>>
一种简单的Failover机制
查看>>
Channel最佳实践之基本规则【译】
查看>>
天下无难试之HTTP协议面试刁难大全
查看>>
深入Python多进程编程基础
查看>>
深入理解RPC——RPC在企业服务中的核心价值
查看>>
跋山涉水 —— 深入 Redis 字典遍历
查看>>
如何解决Java线程池队列过饱问题
查看>>
Lettuce快速入门
查看>>
轻量级框架Spark快速入门
查看>>
蚂蚁金服RPC框架结构分析
查看>>
eclipse踩坑Order and Export引发java.lang.NoClassDefFoundErrorFailed resolution of
查看>>