WinAPI--文件夾創建--CreateDirectory與CreateDirectoryEx

{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;

 

转载于:https://www.cnblogs.com/soicoo/archive/2009/10/20/1586998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值