C++ CreateDirectory()

关于GetSystemDirectory function,参考:https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx

 

以下代码参考:http://blog.csdn.net/co_diy/article/details/6831701,不过他好像不是用C++。

IDE: Microsoft Visual Studio Community 2017 15.5.2

操作系统:Windows 7 x64

 1 #include "stdafx.h"    /* IDE自行创建的 */
 2 
 3 #include <windows.h>
 4 
 5 int main(int argc, char **argv)
 6 {
 7     if (CreateDirectory(L"H:\\C++\\NewDirectory", NULL)) {
 8         // If the function succeeds, the return value is nonzero.
 9         printf("Create a new directory succeeds. \n");
10     }
11     else {
12         // If the function fails, the return value is zero. 
13         // To get extended error information, call GetLastError.
14         // Possible errors include the following.
15         switch (GetLastError())
16         {
17         case ERROR_ALREADY_EXISTS:
18             printf("The specified directory already exists!");
19             break;
20         case ERROR_PATH_NOT_FOUND:
21             printf("One or more intermediate directories do not exist!");
22             break;
23         }
24     }
25 
26     getchar();
27 
28     return 0;
29 }

 

GetSystemDirectory()的第一个参数"H:\\C++\\NewDirectory"的前面需要加L,否则IDE会报错(应该和IDE有关)。

如果你的电脑里不存在目录:H:\C++,程序会提示:“One or more intermediate directories do not exist!”。这时候,你需要重新指定在一个存在的目录作为参数。

如果要创建的文件夹已存在(即文件夹NewDirectory),程序会提示:“The specified directory already exists!”,意思是指定的目录早已存在。

posted @ 2018-01-05 21:09  heismk  阅读(3112)  评论(0编辑  收藏  举报