How do I recursively create a folder in Win32?

前端 未结 14 1248
孤城傲影
孤城傲影 2020-12-01 14:11

I\'m trying to create a function that takes the name of a directory (C:\\foo\\bar, or ..\\foo\\bar\\..\\baz, or \\\\someserver\\foo\\bar

14条回答
  •  有刺的猬
    2020-12-01 14:32

    I'm modifying an old Windows CE app, and this is what I'm planning to use. Should work in Windows CE, too. This is actually recursive, too:

    static void createPath(const CString& p)
    {
       // only create directories that don't exist
       if (::GetFileAttributes(p) == INVALID_FILE_ATTRIBUTES)
       {
          // check if our parent needs to be created, too...
          int i = p.ReverseFind('\\');
          if (i > 0)
          {
             // ...yes, create the parent (recursively)
             createPath(p.Left(i));
          }
    
          // finally, actually create the directory in p
          ::CreateDirectory(p, NULL);
       }
    }
    

提交回复
热议问题