我需要一个 c# 的模拟
Directory.CreateDirectory("d:\\asd\\dsa\\123");
这将创建所有目录,即使磁盘 D 完全为空且没有任何目录。
我阅读了有关 WinApi CreateDirectory 的下一件事:
“ERROR_PATH_NOT_FOUND - 一个或多个中间目录不存在;此函数只会在路径中创建最终目录。”
所以这不是我要找的..
还有其他方法可以做我想做的事吗?
Did you try to use mkdir()
function ? Another way to use:
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";
}
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
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.
检查您的特定编译器供应商是否为此目的提供了自己的 RTL 函数。例如,Delphi/C++Builder 有一个ForceDirectories()
可用的函数。
好吧,在 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);
};