比如给定"c:\ddd\ddd\ddd"就是有效的,给定"c:\ddd\*d*/"就是无效的,不管硬盘上是否存在该文件或者文件夹.
不知道VC里面有没有现成的函数调用?谢谢!!!!

解决方案 »

  1.   

    下面两个函数结合使用应该可以解决你提出的问题:BOOL SetCurrentDirectory(
      LPCTSTR lpPathName   // new directory name
    );BOOL CreateDirectory(
      LPCTSTR lpPathName,                         // directory name
      LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD
    );
      

  2.   

    两个办法:
    1、
        CFileFind f;
        if(f.FindFile("c:\\temp"))
            AfxMessageBox("存在");
            //Directory exist.
        else
            AfxMessageBox("不存在");
            //Not exist.2、
        if(_access("c:\\temp", 0) == 0){
            AfxMessageBox("存在");
        }
        else{
            AfxMessageBox("不存在");
        }
      

  3.   

    jennyvenus:Sorry!我不是想判断文件是否存在
      

  4.   

    _access(_T("c:\\temp\\", 0);可以看一下文件夹是否存在.不光是文件的.
      

  5.   

    如果你硬要判断的话,你就一个一个的把字符串中的字符读出来,如果碰到:/ \ : * ? " < > | 这些符号就说明这个路径是错误的。但/可以不算,因为它是路径分隔符。
      

  6.   

    在Shlwapi.dll中有许多函数,像PathIsDirectoryA,PathIsDirectoryEmptyW,PathIsFileSpecW,PathIsNetworkPathW,PathIsRootA,PathIsRelativeW,够不够用
      

  7.   

    windows不语序文件名中带了非法字符
    所以用pathisdirectory就找不到该文件,所以你创建这种文件名的文件就会创建失败
      

  8.   

    难道你们都没有明白我的意思吗?首先假设存在这个目录,然后我们用SetCurrentDirectory( ),如果失败,那么那么是不存在这个目录,要么是路径不正确。然后我们用CreateDirectory( ),如果不成功那么就否定了不存在这个目录。那就说明了是路径本身不正确的嘛。我第一个给出了正确答案,你们居然还没有弄懂我的思路!下面两个函数结合使用应该可以解决你提出的问题:BOOL SetCurrentDirectory(
      LPCTSTR lpPathName   // new directory name
    );BOOL CreateDirectory(
      LPCTSTR lpPathName,                         // directory name
      LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD
    );
      

  9.   

    StdAfx:CreateDirectory只能创建一层新的路径比如C盘下面没有ccc的目录如果我们CreateDirectory("c:\\ccc\\ccc",NULL)就会出错所以你的方法不行
      

  10.   

    顺便再问一下,怎么一次性创建多层的目录,比如在c盘下创建ccc,再在ccc下创建ddd
      

  11.   

    #include "stdafx.h"
    #include <shellapi.h>
    #include <io.h>
    #include <sys/stat.h>
    #include <direct.h>#include "Category.h"
    #include "HttpCar.h"BOOL File::create(LPCTSTR p, __int64 s)
    {
    BOOL ret = FALSE;
    // ULONG  h = (s & 0xffffffff00000000) >> 32;
    // ULONG  l = (s & 0xffffffff); this->path = p; if (-1 == (ret = _taccess(p, 0))) {

    if (FALSE == mkDir(p)) {
    return FALSE;
    } INT hFile = _tcreat(p, _S_IREAD | _S_IWRITE);
    if (hFile != -1) {
    if (0 == _chsize(hFile, s)) {
    _close(hFile);
    return ret = TRUE;
    } else {
    _close(hFile);
    return ret;
    }
    } // below method use memory mapping file creae "large" files
    // HANDLE hsrcfile = CreateFile(p, 
    // GENERIC_READ | GENERIC_WRITE ,
    // FILE_SHARE_READ | FILE_SHARE_WRITE,
    // NULL,
    // CREATE_NEW,
    // FILE_ATTRIBUTE_NORMAL,
    // NULL);
    // ASSERT(hsrcfile != NULL);
    //
    // HANDLE hmap = CreateFileMapping(hsrcfile, 
    // NULL, 
    // PAGE_READWRITE,
    // h, l, NULL);
    // ASSERT(hmap);
    //
    // LPVOID  pfile = MapViewOfFile(hmap,
    // FILE_MAP_ALL_ACCESS,
    // 0, 0, 0);
    // ASSERT(pfile != NULL);
    //
    // ULONG writed;
    // ret = WriteFile(hsrcfile, pfile, s, &writed, NULL);
    // ASSERT(ret);
    //
    // UnmapViewOfFile(pfile);
    // CloseHandle(hmap);
    // CloseHandle(hsrcfile);

    ret = TRUE;
    } else if (ret == 0) {
    ret = TRUE;
    }
    return ret;
    }ULONG File::write(LPCTSTR content, ULONG contentsize, CarInfo *ci)
    {
    ASSERT(ci != NULL);

    ULONG ret = 0L; FILE *fp = fopen(path, "r+b");
    if (fp)
    {
    fseek(fp, ci->cur, SEEK_SET);
    fwrite((LPVOID)(content), sizeof(TCHAR),
    contentsize, fp);
    fclose(fp);

    ci->cur += contentsize;
    return contentsize;
    }
    else
    {
    return 0L;
    }}BOOL File::mkDir(LPCTSTR path)
    {
    BOOL ret = FALSE;

    using std::string; string s(path);
    string dir;
    string::size_type pos, pos_pre = 0;
    pos = pos_pre; // check if dir exist
    char *p = strrchr(s.c_str(), '\\');
    dir = s.substr(0, p - s.c_str() + 1); if (0 == access(dir.c_str(), 0)) {
    return TRUE;
    }

    pos = s.find_first_of('\\');
    while (pos != string::npos) {
    dir = s.substr(0, pos + 1); ++pos;
    if (access(dir.c_str(), 0) == -1) {
    // make dir
    if (_mkdir(dir.c_str()) != 0) {
    return FALSE;
    }
    }
    pos = s.find('\\', pos);
    } // while return TRUE;
    }