[原创]Delphi 目录函数:ForceDirectories 和 CreateDir 的详细介绍
<p><span style="font-size: 16px"><strong>[原创]Delphi 目录函数:ForceDirectories 和 CreateDir 的详细介绍<br></strong></span></p><p><span style="font-size: 16px">引用单元:SysUtils</span></p>
<p><span style="font-size: 16px"><strong>1、CreateDir 创建一个新目录</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">function CreateDir(const Dir: string): Boolean; //创建一级目录父目录必需存在</span></pre>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">function CreateDir(const Dir: string): Boolean;
begin
Result := CreateDirectory(PChar(Dir), nil);
end;</span></pre>
</div>
<p><span style="font-size: 16px">返回值:如果成功创建了新目录,则返回值为true;如果发生错误,则返回值为false。</span></p>
<p><span style="font-size: 16px"><strong>2、ForceDirectories 创建新目录,包括根据需要创建父目录。</strong></span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">function ForceDirectories(Dir: string): Boolean; //创建多级目录父目录不必存在 (Force 有暴力、强制的意思) </span></pre>
</div>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">function ForceDirectories(Dir: string): Boolean;
var
E: EInOutError;
begin
Result := True;
if Dir = '' then
begin
E := EInOutError.CreateRes(@SCannotCreateDir);
E.ErrorCode := 3;
raise E;
end;
Dir := ExcludeTrailingPathDelimiter(Dir);
{$IFDEF MSWINDOWS}
if (Length(Dir) < 3) or DirectoryExists(Dir)
or (ExtractFilePath(Dir) = Dir) then Exit; // avoid 'xyz:\' problem.
{$ENDIF}
{$IFDEF LINUX}
if (Dir = '') or DirectoryExists(Dir) then Exit;
{$ENDIF}
Result := ForceDirectories(ExtractFilePath(Dir)) and CreateDir(Dir);
end;
</span></pre>
</div>
<p><span style="font-size: 16px">ForceDirectory按照Dir中的指定创建一个新目录,该目录必须是完全限定的路径名。如果路径中给出的目录尚不存在,ForceDirectory会尝试创建它们。</span></p>
<p><span style="font-size: 16px">返回值:如果ForceDirectory成功创建了所有必要的目录,则返回true;如果无法创建所需的目录,则返回false。</span></p>
<p><span style="font-size: 16px">重要提示:不要使用空字符串调用ForceDirectory。这样做会导致ForceDirectory引发异常。</span></p>
<p><span style="font-size: 16px">FileCtrl单元(仅限Windows)还包含ForceDirectory函数。但是,不推荐使用FileCtrl版本,而首选SysUtils版本,即使代码不需要跨平台。</span></p>
<p><span style="font-size: 16px"><strong>3、<strong>CreateDir 和 <strong>ForceDirectories 的</strong> </strong>区别</strong></span></p>
<p><span style="font-size: 16px">//例如:现有目录:D:\目录1</span></p>
<p><span style="font-size: 16px">//想要创建:D:\目录1\目录2\目录3\目录4 不存在的文件目录有 :目录2\目录3\目录4 </span></p>
<p><span style="font-size: 16px"><strong>ForceDirectories 函数 </strong>会同时创建这三个目录 目录2、目录3、目录4</span></p>
<p><span style="font-size: 16px"><strong>CreateDir 函数 </strong>如果不存在 目录2 这个目录,则返回创建 [目录4] 文件夹失败</span></p>
<p> </p>
<p><span style="font-size: 16px"><strong>4、示例:</strong></span></p>
<p><span style="font-size: 16px">4.1 CreateDir</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">uses FileCtrl;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not DirectoryExists('c:\temp') then
if not CreateDir('C:\temp') then
raise Exception.Create('不能创建 c:\temp');
end;</span></pre>
</div>
<p><span style="font-size: 16px">4.2 ForceDirectories</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure TForm1.Button1Click(Sender: TObject);
var
Dir: string;
begin
Dir := 'C:\目录1\目录2\目录3';
if ForceDirectories(Dir) then
Label1.Caption := Dir + ' 已经创建';
end;
</span></pre>
</div>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 16px"> </span></p>
<p><span style="font-size: 14px; color: rgba(136, 136, 136, 1)">创建时间:2019.07.30 更新时间:2022.02.16</span></p>
</div>
<div id="MySignature" role="contentinfo">
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/11271102.html 感谢楼主的详细分享!这两个函数确实是Delphi中常用的目录操作函数,讲解得非常清晰[:)]
我来补充一点使用心得:
关于ForceDirectories的路径处理
var
Dir: string;
begin
Dir := 'C:\Test\Folder1\Folder2';
// 即使Folder1不存在,ForceDirectories也会自动创建
if ForceDirectories(Dir) then
ShowMessage('目录创建成功');
end;
一个小提示
在实际开发中,我通常会这样使用:
var
TargetPath: string;
begin
TargetPath := ExtractFilePath(Application.ExeName) + 'Data\Config';
// 使用ForceDirectories更安全,避免因父目录不存在而失败
if not ForceDirectories(TargetPath) then
raise Exception.Create('无法创建目录:' + TargetPath);
end;
个人建议:如果确定父目录一定存在,可以用CreateDir,性能会稍好一点点。但为了代码的健壮性,大多数情况下用ForceDirectories更稳妥,毕竟没人能保证目录一定存在[:)]
另外,楼主提到的FileCtrl单元确实已经不推荐使用了,SysUtils版本是跨平台的,这个提醒很到位!
感谢原创,收藏了!
頁:
[1]