{Windows API:}
BOOL CreateDirectory( //原聲明
LPCTSTR lpPathName, //新目錄
LPSECURITY_ATTRIBUTES lpSecurityAttributes //定义了目录的安全特性結構指針
);
{Delphi中:}
//声明:
CreateDirectory(
lpPathName: PChar; //新目錄
lpSecurityAttributes: PSecurityAttributes //定义了目录的安全特性 TSecurityAttributes 结构的指针
): BOOL;
//TSecurityAttributes 是 _SECURITY_ATTRIBUTES 结构的重定义
_SECURITY_ATTRIBUTES = record
nLength: DWORD; //结构体的大小
lpSecurityDescriptor: Pointer; //安全描述
bInheritHandle: BOOL; //安全描述的对象能否被新创建的进程继承
end;
//CreateDirectory 返回類型為Boolean
//===============================================================
{Windows API:}
BOOL CreateDirectoryEx(
LPCTSTR lpTemplateDirectory, // 模板的目錄
LPCTSTR lpNewDirectory, // 新目錄
LPSECURITY_ATTRIBUTES lpSecurityAttributes //定义了目录的安全特性結構指針
);
{Delphi:}
CreateDirectoryEx(
lpTemplateDirectory, lpNewDirectory: PChar;//如上
lpSecurityAttributes: PSecurityAttributes
): BOOL
//與上的區別就是多了一個模板參數,指定一个模板目录的名字,从中复制默认属性(比如目录中文件的默认压缩方式)。
//? 網上有人解釋“如设为vbNullString(Delphi應該是Nil吧),则表示不使用模板<http://baike.baidu.com/view/2034858.htm>”,但是經過實際在D7中驗證,當模板目錄不存在或設為nil時不創建新目錄。
//====================================================================
{Delphi Example}
var
sTPath: string;
pPath1: string;
pPath2: string;
begin
sTPath := 'E:\aa';
pPath1 := 'E:\bb';
pPath2 := 'E:\cc';
CreateDirectory(PChar(pPath1),nil);
//CreateDirectoryEx(PChar(sTPath),PChar(pPath2),nil); //創建的文件夾的'可讀'、'存檔'等特性確實與模板一樣
//CreateDirectoryEx(nil,PChar(pPath2),nil); //?實際不創建目錄
//CreateDirectoryEx('..',PChar(pPath2),nil); //'..'表示當前目錄的上層目錄
CreateDirectoryEx('..',PChar(pPath2),nil); //'.'表示當前目錄
end;
//=============================================
{另外還有一個Delphi自帶的創建目錄的過程----ForceDirectories(與上邊過程不同之處在于:以上過程當某級目錄不存在時,創建目錄會不成功,而該過程會創建各級目錄,帶有強制性)}
{Delphi Example}
var
pPath1: string;
begin
pPath1 := 'E:\bb\cc\dd';
ForceDirectories(pPath1); //即使dd文件夾的上層文件夾cc和bb文件夾不存在也會創建
end;