delphi 路径操作函数
<h1 id="路径操作函数">路径操作函数</h1><h3 id="systemsysutilsansicomparefilename">System.SysUtils.AnsiCompareFileName</h3>
<p>根据当前语言环境比较文件名。</p>
<blockquote>
<p>在 <strong>Windows</strong> 下不区分大小写,在 <strong>MAC OS</strong> 下区分大小写。</p>
<p>在不使用多字节字符集 (<strong>MBCS</strong>) 的 <strong>Windows</strong> 区域设置下,<code>AnsiCompareFileName</code> 与 <code>AnsiCompareText</code> 相同。在 <strong>MAC OS</strong> 和 <strong>Linux</strong> 下,<code>AnsiCompareFileName</code> 与 <code>AnsiCompareStr</code> 相同。</p>
</blockquote>
<pre><code class="language-delphi">begin
if SameFileName('D:\ceshi\新建文件夹\ceshi.txt', 'D:\ceshi\新建文件夹\ceshi.txt') then
Memo1.Lines.Add('文件名相同');
//输出 文件名相同
if AnsiCompareFileName('ceshi.txt', 'CESHI.txt') = 0 then
Memo1.Lines.Add('相等,Windows下不区分大小写')
else
Memo1.Lines.Add('不相等');
//输出 相等,Windows下不区分大小写
end;
</code></pre>
<h3 id="systemsysutilsansilowercasefilename">System.SysUtils.AnsiLowerCaseFileName</h3>
<p>将文件名转换为小写。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(AnsiLowerCaseFileName('CESHI.txt'));
//输出 ceshi.txt
end;
</code></pre>
<h3 id="systemioutilstpathchangeextension">System.IOUtils.TPath.ChangeExtension</h3>
<pre><code class="language-delphi">class function ChangeExtension(const Path, Extension: string): string;
</code></pre>
<p>更改给定路径指示的文件或目录的扩展名。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.ChangeExtension('C:\Users\Administrator\Desktop\ceshi.txt', 'xml'));
//输出 C:\Users\Administrator\Desktop\ceshi.xml
end;
</code></pre>
<h3 id="systemsysutilschangefileext">System.SysUtils.ChangeFileExt</h3>
<pre><code class="language-delphi">function ChangeFileExt(const FileName, Extension: string): string;
</code></pre>
<p>更改文件名的扩展名。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ChangeFileExt('ceshi.txt', '.xml'));
//输出 ceshi.xml
end;
</code></pre>
<h3 id="systemsysutilschangefilepath">System.SysUtils.ChangeFilePath</h3>
<p>更改文件名的路径。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ChangeFilePath('C:\Users\Administrator\Desktop\ceshi.txt', 'D:\'));
//输出 D:\ceshi.txt
end;
</code></pre>
<h3 id="systemsysutilsdirectoryexists">System.SysUtils.DirectoryExists</h3>
<p>判断指定目录是否存在。</p>
<pre><code class="language-delphi">begin
if DirectoryExists('D:\ceshi') then
Memo1.Lines.Add('文件目录存在')
else
Memo1.Lines.Add('文件目录不存在');
end;
</code></pre>
<h3 id="systemioutilstpathcombine">System.IOUtils.TPath.Combine</h3>
<p>组合两个路径字符串。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.Combine('D:\ceshi\新建文件夹', '新建文件夹 (2)'));
//输出 D:\ceshi\新建文件夹\新建文件夹 (2)
end;
</code></pre>
<h3 id="systemioutilstpathdriveexists">System.IOUtils.TPath.DriveExists</h3>
<p>检查给定路径中使用的驱动器号是否实际存在。</p>
<pre><code class="language-delphi">var
vPath: string;
begin
vPath := 'D:\ceshi2';
if TPath.DriveExists(vPath) then
begin
Memo1.Lines.Add('驱动器号存在');
if not DirectoryExists(vPath) then
Memo1.Lines.Add('文件目录不存在');
end;
end;
</code></pre>
<h3 id="systemsysutilsexcludetrailingbackslash">System.SysUtils.ExcludeTrailingBackslash</h3>
<p>返回不带尾部分隔符的路径名。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ExcludeTrailingBackslash('D:\ceshi\新建文件夹\'));
//输出 D:\ceshi\新建文件夹
end;
</code></pre>
<h3 id="systemsysutilsexcludetrailingpathdelimiter">System.SysUtils.ExcludeTrailingPathDelimiter</h3>
<p>返回不带尾部分隔符的路径名。</p>
<p>与 <code>ExcludeTrailingBackslash</code> 相同。</p>
<h3 id="systemsysutilsexpandfilename">System.SysUtils.ExpandFileName</h3>
<p>返回相对文件名的完整路径名。</p>
<pre><code class="language-delphi">begin
//相对于程序运行路径的完整路径
Memo1.Lines.Add(ExpandFileName('新建文件夹\ceshi'));
//输出 D:\Project1\Win32\Debug\新建文件夹\ceshi
end;
</code></pre>
<h3 id="systemsysutilsexpandfilenamecase">System.SysUtils.ExpandFileNameCase</h3>
<p>返回区分大小写的文件系统上相对文件名的完整路径名。</p>
<p>在 <strong>Windows</strong> 下与 <code>ExpandFileName</code> 相同。在 <strong>MAC OS</strong> 和 <strong>Linux</strong> 下查找文件</p>
<pre><code class="language-delphi">uses System.TypInfo;
var
vMatch: TFilenameCaseMatch;
begin
Memo1.Lines.Add(ExpandFileNameCase('ceshi.txt', vMatch));
//输出 C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Win32\Debug\ceshi.txt
Memo1.Lines.Add(GetEnumName(TypeInfo(TFilenameCaseMatch), Ord(vMatch)));
//输出 mkNone
end;
</code></pre>
<h3 id="systemsysutilsexpanduncfilename">System.SysUtils.ExpandUNCFileName</h3>
<p>如果合适,以 UNC 格式返回文件名的完整路径。</p>
<pre><code class="language-delphi">begin
//在“网络位置”中
Memo1.Lines.Add(ExpandUNCFileName('ceshi.txt'));
//输出 \\192.168.1.1\ceshi\ceshi.txt
end;
</code></pre>
<h3 id="systemsysutilsextractfiledir">System.SysUtils.ExtractFileDir</h3>
<p>从文件名中提取驱动器和目录部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ExtractFileDir('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 D:\ceshi\新建文件夹
end;
</code></pre>
<h3 id="systemsysutilsextractfiledrive">System.SysUtils.ExtractFileDrive</h3>
<p>返回文件名的驱动器部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ExtractFileDrive('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 D:
end;
</code></pre>
<h3 id="systemsysutilsextractfileext">System.SysUtils.ExtractFileExt</h3>
<p>返回文件名的扩展名部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ExtractFileExt('ceshi.txt'));
//输出 .txt
Memo1.Lines.Add(ExtractFileExt('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 .txt
end;
</code></pre>
<h3 id="systemsysutilsextractfilename">System.SysUtils.ExtractFileName</h3>
<p>提取文件名的名称和扩展名部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ExtractFileName('ceshi.txt'));
//输出 ceshi.txt
Memo1.Lines.Add(ExtractFileName('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 ceshi.txt
end;
</code></pre>
<h3 id="systemsysutilsextractfilepath">System.SysUtils.ExtractFilePath</h3>
<p>返回文件名的驱动器和目录部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(ExtractFilePath('ceshi.txt'));
//输出
Memo1.Lines.Add(ExtractFilePath('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 D:\ceshi\新建文件夹\
end;
</code></pre>
<h3 id="systemsysutilsextractrelativepath">System.SysUtils.ExtractRelativePath</h3>
<p>返回相对于特定基目录的相对路径名。</p>
<pre><code class="language-delphi">begin
//路径需要带“\”,否则返回错误
ExtractRelativePath('D:\ceshi\新建文件夹\', 'D:\ceshi\新建文件夹 (2)\');
//输出 ..\新建文件夹 (2)\
end;
</code></pre>
<h3 id="systemsysutilsextractshortpathname">System.SysUtils.ExtractShortPathName</h3>
<p>将文件名转换为简短的8.3格式。</p>
<pre><code class="language-delphi">begin
//文件必须存在才返回
Memo1.Lines.Add(ExtractShortPathName('C:\Program Files (x86)\Embarcadero\Studio\22.0\bin\bds.exe'));
//输出 C:\PROGRA~2\EMBARC~1\Studio\22.0\bin\bds.exe
end;
</code></pre>
<h3 id="systemioutilstpathgetattributes">System.IOUtils.TPath.GetAttributes</h3>
<pre><code class="language-delphi">class function GetAttributes(const Path: string; FollowLink: Boolean = True): TFileAttributes;
</code></pre>
<p>返回文件或目录属性。</p>
<pre><code class="language-delphi">uses System.IOUtils, System.TypInfo;
var
vAttributes: TFileAttributes;
vAttrib: TFileAttribute;
begin
vAttributes := TPath.GetAttributes('D:\ceshi\新建文件夹\');
for vAttrib in vAttributes do
Memo1.Lines.Add(GetEnumName(TypeInfo(TFileAttribute), Ord(vAttrib)));
//输出 faDirectory
vAttributes := TPath.GetAttributes('D:\ceshi\新建文件夹\ceshi.txt');
for vAttrib in vAttributes do
Memo1.Lines.Add(GetEnumName(TypeInfo(TFileAttribute), Ord(vAttrib)));
//输出 GetAttributes
end;
</code></pre>
<h3 id="systemioutilstpathgetdirectoryname">System.IOUtils.TPath.GetDirectoryName</h3>
<p>提取文件名的驱动器和目录部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add( TPath.GetDirectoryName('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 D:\ceshi\新建文件夹
end;
</code></pre>
<h3 id="systemioutilstpathgetextendedprefix">System.IOUtils.TPath.GetExtendedPrefix</h3>
<p>返回给定路径的扩展前缀类型。</p>
<pre><code class="language-delphi">begin
TPath.GetExtendedPrefix('D:\ceshi\新建文件夹\');
//输出 pptNoPrefix
end;
</code></pre>
<h3 id="systemioutilstpathgetextension">System.IOUtils.TPath.GetExtension</h3>
<p>提取文件名的扩展名部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetExtension('ceshi.txt'));
//输出 .txt
Memo1.Lines.Add(TPath.GetExtension('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 .txt
end;
</code></pre>
<h3 id="systemioutilstpathgetfilename">System.IOUtils.TPath.GetFileName</h3>
<p>提取文件名的名称和扩展名部分。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetFileName('ceshi.txt'));
//输出 ceshi.txt
Memo1.Lines.Add(TPath.GetFileName('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 ceshi.txt
end;
</code></pre>
<h3 id="systemioutilstpathgetfilenamewithoutextension">System.IOUtils.TPath.GetFileNameWithoutExtension</h3>
<p>提取文件名的名称部分,不带扩展名。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetFileNameWithoutExtension('ceshi.txt'));
//输出 ceshi
Memo1.Lines.Add(TPath.GetFileNameWithoutExtension('D:\ceshi\新建文件夹\ceshi.txt'));
//输出 ceshi
end;
</code></pre>
<h3 id="systemioutilstpathgetfullpath">System.IOUtils.TPath.GetFullPath</h3>
<p>返回给定路径的绝对路径。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetFullPath('ceshi.txt'));
//输出 C:\Users\Administrator\Documents\Embarcadero\Studio\Projects\Win32\Debug\ceshi.txt
end;
</code></pre>
<h3 id="systemioutilstpathgetguidfilename">System.IOUtils.TPath.GetGUIDFileName</h3>
<p>生成可用作唯一文件名的新 GUID。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetGUIDFileName);
//输出 17DC1DDB8C334D61A5499597CEC22D5E
end;
</code></pre>
<h3 id="systemioutilstpathgethomepath">System.IOUtils.TPath.GetHomePath</h3>
<p>返回用户的主路径。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetHomePath);
//输出 C:\Users\Administrator\AppData\Roaming
end;
</code></pre>
<h3 id="systemioutilstpathgetrandomfilename">System.IOUtils.TPath.GetRandomFileName</h3>
<p>生成新的随机文件名。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetRandomFileName);
//输出 0EpRoGU5.7O1
end;
</code></pre>
<h3 id="systemioutilstpathgettempfilename">System.IOUtils.TPath.GetTempFileName</h3>
<p>生成一个唯一的临时文件。</p>
<pre><code class="language-delphi">var
vFile: string;
begin
vFile := TPath.GetTempFileName;
Memo1.Lines.Add(vFile);
//输出 C:\Users\Administrator\AppData\Local\Temp\tmp8F56.tmp
//不使用时删除
DeleteFile(vFile);
end;
</code></pre>
<h3 id="systemioutilstpathgettemppath">System.IOUtils.TPath.GetTempPath</h3>
<p>返回系统临时目录的路径。</p>
<pre><code class="language-delphi">begin
Memo1.Lines.Add(TPath.GetTempPath);
//输出 C:\Users\Administrator\AppData\Local\Temp\
end;
</code></pre>
<h3 id="systemioutilstpathhasextension">System.IOUtils.TPath.HasExtension</h3>
<p>检查给定文件名是否有扩展名部分。</p>
<pre><code class="language-delphi">begin
if TPath.HasExtension('ceshi.txt') then
Memo1.Lines.Add('包含扩展名');
//输出 包含扩展名
if not TPath.HasExtension('D:\ceshi\新建文件夹\') then
Memo1.Lines.Add('不包含扩展名');
//输出 不包含扩展名
end;
</code></pre>
<h3 id="systemioutilstpathhasvalidfilenamechars">System.IOUtils.TPath.HasValidFileNameChars</h3>
<pre><code class="language-delphi">class function HasValidFileNameChars(const FileName: string; const UseWildcards: Boolean): Boolean;
</code></pre>
<p>检查给定文件名是否仅包含允许的字符。</p>
<p><em>UseWildcards</em> 指定通配符是否被视为有效的文件名字符(例如星号或问号)。</p>
<pre><code class="language-delphi">begin
if not TPath.HasValidFileNameChars('<ceshi>.txt', False) then
Memo1.Lines.Add('包含特殊字符');
//输出 包含特殊字符
if TPath.HasValidFileNameChars('ceshi*.txt', True) then
Memo1.Lines.Add('包含通配符字符');
//输出 包含通配符字符
end;
</code></pre>
<h3 id="systemioutilstpathhasvalidpathchars">System.IOUtils.TPath.HasValidPathChars</h3>
<pre><code class="language-delphi">class function HasValidPathChars(const Path: string; const UseWildcards: Boolean): Boolean;
</code></pre>
<p>检查给定路径字符串是否仅包含允许的字符。</p>
<p><em>UseWildcards</em> 指定通配符是否被视为有效的文件名字符(例如星号或问号)。</p>
<pre><code class="language-delphi">begin
if not TPath.HasValidPathChars('D:\ceshi\<新建文件夹>', False) then
Memo1.Lines.Add('包含特殊字符');
//输出 包含特殊字符
if TPath.HasValidPathChars('D:\ceshi\新建文件夹*', True) then
Memo1.Lines.Add('包含通配符字符');
//输出 包含通配符字符
end;
</code></pre>
<h3 id="systemsysutilsincludetrailingbackslash">System.SysUtils.IncludeTrailingBackslash</h3>
<p>返回带尾部分隔符的路径名。(在 Windows 上为 <code>\</code>,否则为 <code>/</code>)。</p>
<blockquote>
<p>注意:包含此函数只是为了向后兼容。应改用 <code>System.SysUtils.IncludeTrailingPathDelimiter</code>。</p>
</blockquote>
<h3 id="systemsysutilsincludetrailingpathdelimiter">System.SysUtils.IncludeTrailingPathDelimiter</h3>
<pre><code class="language-delphi">function IncludeTrailingPathDelimiter(const S: string): string;
</code></pre>
<p>返回带尾部分隔符的路径名。(在 Windows 上为 <code>\</code>,否则为 <code>/</code>)。</p>
<p>如果已经以尾部分隔符结尾,则原样返回;否则,附加分隔符。</p>
<pre><code class="language-delphi">procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(IncludeTrailingPathDelimiter('D:\ceshi\新建文件夹'));
//输出 D:\ceshi\新建文件夹\
end;
</code></pre>
<h3 id="systemioutilstpathisdriverooted">System.IOUtils.TPath.IsDriveRooted</h3>
<pre><code class="language-delphi">class function IsDriveRooted(const Path: string): Boolean;
</code></pre>
<p>检查给定路径是否是绝对路径,并以驱动器号开头。</p>
<blockquote>
<p>在 POSIX 上,始终返回 <em>false</em>,因为没有驱动器根。</p>
</blockquote>
<pre><code class="language-delphi">begin
if TPath.IsDriveRooted('D:\ceshi\新建文件夹\') then
Memo1.Lines.Add('绝对路径');
//输出 绝对路径
if TPath.IsDriveRooted('D:') then
Memo1.Lines.Add('绝对路径');
//输出 绝对路径
end;
</code></pre>
<h3 id="systemioutilstpathisextendedprefixed">System.IOUtils.TPath.IsExtendedPrefixed</h3>
<pre><code class="language-delphi">class function IsExtendedPrefixed(const Path: string): Boolean;
</code></pre>
<p>检查给定路径是否包含扩展前缀。</p>
<p>以 <code>\\?\</code>或 <code>\\?\UNC\</code> 为前缀的路径是 Windows 特有的,长度可以非常大,并且不限于 255 个字符 (MAX_PATH)。在路径前面加上 <code>\\?\</code> 可以解决长度超过 255 个字符问题。<code>\\?\</code> 告诉 Windows API 禁用所有字符串解析并将其后面的字符串发送到文件系统。可以超出 Windows API 强制执行的 MAX_PATH 限制。</p>
<blockquote>
<p>在 POSIX 上,始终返回 <em>false</em>,因为没有扩展前缀。</p>
</blockquote>
<pre><code class="language-delphi">begin
if TPath.IsExtendedPrefixed('\\?\D:\ceshi\新建文件夹') then
Memo1.Lines.Add('包含扩展前缀');
//输出 包含扩展前缀
end;
</code></pre>
<h3 id="systemsysutilsispathdelimiter">System.SysUtils.IsPathDelimiter</h3>
<pre><code class="language-delphi">function IsPathDelimiter(const S: string; Index: Integer): Boolean;
</code></pre>
<p>指示字符串中指定位置的字符是否为路径分隔符。(在 Windows 上为 <code>\</code>,否则为 <code>/</code>)。位置索引从 <em>1</em> 开始。</p>
<pre><code class="language-delphi">begin
if IsPathDelimiter('D:\ceshi\新建文件夹', 3) then
Memo1.Lines.Add('第3个字符为路径分隔符\');
//输出 第3个字符为路径分隔符\
end;
</code></pre>
<h3 id="systemioutilstpathispathrooted">System.IOUtils.TPath.IsPathRooted</h3>
<p>检查给定路径是否是绝对路径。</p>
<pre><code class="language-delphi">begin
if TPath.IsPathRooted('D:\ceshi\新建文件夹\') then
Memo1.Lines.Add('绝对路径');
//输出 绝对路径
if TPath.IsPathRooted('\ceshi\新建文件夹') then
Memo1.Lines.Add('没有驱动器号开头的绝对路径');
//输出 没有驱动器号开头的绝对路径
if not TPath.IsPathRooted('ceshi\新建文件夹') then
Memo1.Lines.Add('相对路径');
//输出 绝对路径
end;
</code></pre>
<h3 id="systemioutilstpathisuncpath">System.IOUtils.TPath.IsUNCPath</h3>
<p>检查给定路径是否为 UNC 格式。UNC 格式路径以两个反斜杠字符为前缀(例如<code>\\computer\folder</code>)。</p>
<pre><code class="language-delphi">begin
if TPath.IsUNCPath('\\192.168.1.1\ceshi') then
Memo1.Lines.Add('UNC格式');
//输出 UNC格式
if not TPath.IsUNCPath('\\192.168.1.1\<ceshi>') then
Memo1.Lines.Add('不正确的UNC格式');
//输出 不正确的UNC格式
end;
</code></pre>
<h3 id="systemioutilstpathisuncrooted">System.IOUtils.TPath.IsUNCRooted</h3>
<p>检查给定路径是否是 UNC 根路径。</p>
<blockquote>
<p>在 POSIX 上,始终返回 <em>false</em>,因为没有扩展前缀。</p>
</blockquote>
<pre><code class="language-delphi">procedure TForm1.Button1Click(Sender: TObject);
begin
if TPath.IsUNCRooted('\\192.168.1.1\ceshi') then
Memo1.Lines.Add('UNC根路径');
//输出 UNC根路径
if TPath.IsUNCRooted('\\192.168.1.1\<ceshi>') then
Memo1.Lines.Add('UNC根路径');
//输出 UNC根路径
end;
</code></pre>
<h3 id="systemioutilstpathisvalidfilenamechar">System.IOUtils.TPath.IsValidFileNameChar</h3>
<p>检查文件名字符串中是否允许使用给定字符。</p>
<blockquote>
<p>Windows 中特殊字符</p>
<pre><code>#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16,#17, #18, #19, #20, #21, #22, #23, #24, #25, #26, #27, #28, #29, #30, #31, ",*, /, :, <, >, ?, \, |
</code></pre>
<p>MacOS, iOS, Android,Linux 中特殊字符</p>
<pre><code>#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16,#17, #18, #19, #20, #21, #22, #23, #24, #25, #26, #27, #28, #29, #30, #31, / , ~.
</code></pre>
</blockquote>
<pre><code class="language-delphi">begin
if not TPath.IsValidFileNameChar('*') then
Memo1.Lines.Add('文件名中不能使用');
//输出 文件名中不能使用
end;
</code></pre>
<h3 id="systemioutilstpathisvalidpathchar">System.IOUtils.TPath.IsValidPathChar</h3>
<p>检查路径字符串中是否允许使用给定字符。</p>
<pre><code class="language-delphi">begin
if not TPath.IsValidPathChar('|') then
Memo1.Lines.Add('路径中不能使用');
//输出 路径中不能使用
end;
</code></pre>
<h3 id="vclfilectrlminimizename">Vcl.FileCtrl.MinimizeName</h3>
<pre><code class="language-delphi">function MinimizeName(const Filename: TFileName; Canvas: TCanvas; MaxLen: Integer): TFileName;
</code></pre>
<p>获取可以在有限大小的在画布上绘制的文件名和路径的缩写名称。 缩短 <em>Filename</em>,使其可以在 <em>MaxLen</em> 长度限制内绘制。用点替换文件名路径部分中的目录,直到生成的名称符合指定的像素长度。<em>Canvas</em> 是要呈现缩写名称的画布,用于确定字体规格。</p>
<pre><code class="language-delphi">rebegin
Memo1.Lines.Add(MinimizeName('D:\ceshi\新建文件夹\ceshi.txt', Self.Canvas, 150));
//输出 D:\...\新建文件夹\ceshi.txt
end;
</code></pre>
<h3 id="systemsysutilssamefilename">System.SysUtils.SameFileName</h3>
<p>根据当前区域设置比较文件名。</p>
<p>与 <code>AnsiCompareFileName</code> 相同。</p>
<h3 id="systemioutilstpathsetattributes">System.IOUtils.TPath.SetAttributes</h3>
<pre><code class="language-delphi">class procedure SetAttributes(const Path: string; const Attributes: TFileAttributes);
</code></pre>
<p>设置文件或目录属性。</p>
<pre><code class="language-delphi">procedure TForm1.Button1Click(Sender: TObject);
begin
//设置文件为只读
TPath.SetAttributes('D:\ceshi\新建文件夹\ceshi.txt', );
//设置目录为隐藏
TPath.SetAttributes('D:\ceshi\新建文件夹', );
end;
</code></pre>
<h3 id="systemioutilstfilecreatesymlink">System.IOUtils.TFile.CreateSymLink</h3>
<pre><code class="language-delphi">class function CreateSymLink(const Link, Target: string): Boolean;
</code></pre>
<p>创建符号链接。</p>
<blockquote>
<p><strong>注意:</strong>调用<code>CreateSymLink</code>时目标文件或目录必须存在。</p>
<p>在 <strong>Windows Vista</strong> 及更高版本的 Windows 上使用。</p>
</blockquote>
<pre><code class="language-delphi">procedure TForm1.Button1Click(Sender: TObject);
begin
//相当于CMD命令 mklink "D:\SymLink" "D:\ceshi\新建文件夹"
TFile.CreateSymLink('D:\SymLink', 'D:\ceshi\新建文件夹');
end;
</code></pre>
<h2 id="参考">参考</h2>
<p>创建符号链接</p>
<p>文件名中使用的字符集</p>
<p>Path Manipulation Routines</p><br><br>
来源:https://www.cnblogs.com/txgh/p/17837089.html
頁:
[1]