0

我需要一个 c# 的模拟

Directory.CreateDirectory("d:\\asd\\dsa\\123");

这将创建所有目录,即使磁盘 D 完全为空且没有任何目录。

我阅读了有关 WinApi CreateDirectory 的下一件事:
“ERROR_PATH_NOT_FOUND - 一个或多个中间目录不存在;此函数只会在路径中创建最终目录。”
所以这不是我要找的..

还有其他方法可以做我想做的事吗?

4

3 回答 3

3

Did you try to use mkdir() function ? Another way to use:

  1. boost filesystem: supports standard MAX_PATH size 260.

    const char dir_path[] = "c:\\temp\\cplusplus";

    boost::filesystem::path dir(dir_path);
            if(boost::filesystem::create_directory(dir)) {
                std::cout << "Success" << "\n";
            }
    
  2. SHCreateDirectoryEx function for Win XP(SP2) and Higher. However, it is limited to 247 characters, which is less than the standard MAX_PATH (260) that other Win32 API filesystem functions support

  3. CreateDirectory function : default string size limit for paths of 248 characters. This limit is related to how the CreateDirectory function parses paths. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" prefix to the path.

NOTE: Because most Boost.Filesystem operational functions just pass the contents of a class path object to the Windows API, they do work with the extended-length prefixes. But some won't work, because to the limitations imposed by Windows. -- Boost warning.

于 2013-10-04T18:21:47.887 回答
2

检查您的特定编译器供应商是否为此目的提供了自己的 RTL 函数。例如,Delphi/C++Builder 有一个ForceDirectories()可用的函数。

于 2013-10-04T23:44:07.637 回答
0

好吧,在 Perl/Ruby/Bash 中,你会,

    `/bin/mkdir -p $pathname` #perl
    %x(/bin/mkdir -p #{pathname}) #ruby
    /bin/mkdir -p $pathname #bash

所以你可以唤起系统,

    system("mkdir -p pathname");

添加:

好吧,您想将给定的路径分成几部分并制作每个部分。在 C 中很容易做到(将 char* 和 char[] 更改为 std::string,将 strcat 更改为 += 对于 c++),

int MakeDir( char* pathname )
{
    struct stat sbuf;
    if( stat(pathname, &sbuf) < 0 )
    {
        mkdir(pathname,0); //set your permissions as you like in 2nd argument
        return(0);
    }
    else //exists? skip
    {
        //stat.st_mode tells file or dir
        if( S_ISDIR(stat.st_mode) ) { return(0); }
        else if( S_ISREG(stat.st_mode) ) { return(-1); }
        else if( S_ISFIFO(stat.st_mode) ) { return(-1); }
        else if( S_LNK(stat.st_mode) ) { return(0); } //can link to dir
        else { return(-1); }
    }
    return(0);
};
////char PATHSEP = "\/"; //unix/linux //not needed, just use 'mkdir -p'
char PATHSEP = "\\"; //windows
int MkdirPath( char *pathname )
{
    char parts = strdup(pathname);
    char buildpath[strlen(pathname)] = "";
    char* part = strtok(parts,PATHSEP);
    while ( part )
    {
        strcat(pathname, PATHSEP); strcat(pathname, part);
        if( MakeDir( pathname ) < 0 ) { break; }
        part = strtok(NULL,PATHSEP);
    }
    return(0);
};
于 2013-10-04T18:40:58.040 回答