[英]Create Directory + Sub Directories
I've got a directory location, how can I create all the directories? 我有一个目录位置,如何创建所有目录? eg C:\\Match\\Upload will create both Match and the sub-directory Upload if it doesn't exist.
例如C:\\ Match \\ Upload将创建Match和子目录Upload(如果它不存在)。
Using C# 3.0 使用C#3.0
Thanks 谢谢
Directory.CreateDirectory (@"C:\\Match\\Upload") will sort this all out for you. Directory.CreateDirectory (@“C:\\ Match \\ Upload”)将为您排序。 You don't need to create all the subdirectories!
您不需要创建所有子目录! The create directory method creates all directories and sub directories for you.
create directory方法为您创建所有目录和子目录。
if (!System.IO.Directory.Exists(@"C:\Match\Upload"))
{
System.IO.Directory.CreateDirectory(@"C:\Match\Upload");
}
for googlers: in pure win32/C++, use SHCreateDirectoryEx 对于googlers:在纯win32 / C ++中,使用SHCreateDirectoryEx
inline void EnsureDirExists(const std::wstring& fullDirPath)
{
HWND hwnd = NULL;
const SECURITY_ATTRIBUTES *psa = NULL;
int retval = SHCreateDirectoryEx(hwnd, fullDirPath.c_str(), psa);
if (retval == ERROR_SUCCESS || retval == ERROR_FILE_EXISTS || retval == ERROR_ALREADY_EXISTS)
return; //success
throw boost::str(boost::wformat(L"Error accessing directory path: %1%; win32 error code: %2%")
% fullDirPath
% boost::lexical_cast<std::wstring>(retval));
//TODO *djg* must do error handling here, this can fail for permissions and that sort of thing
}
Here is an example with a DirectoryInfo
object that will create the directory and all subdirectories: 以下是
DirectoryInfo
对象的示例,该对象将创建目录和所有子目录:
var path = @"C:\Foo\Bar";
new System.IO.DirectoryInfo(path).Create();
Calling Create()
will not error if the path already exists. 如果路径已存在,则调用
Create()
不会出错。
If it is a file path you can do: 如果是文件路径,您可以执行以下操作:
var path = @"C:\Foo\Bar\jazzhands.txt";
new System.IO.FileInfo(path).Directory.Create();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.