如何解决如何检查给定的路径是FILE还是FOLDER?
有某种方法可以精确确定给定路径是FILE还是FOLDER?
如果是,请举个例子吗?预先感谢。
解决方法
您可以使用RTL的TPath.GetAttributes()
,TFile.GetAttributes()
或TDirectory.GetAttributes()
方法,例如:
uses
...,System.IOUtils;
try
if TFileAttribute.faDirectory in TPath{|TFile|TDirectory}.GetAttributes(path) then
begin
// path is a folder ...
end else
begin
// path is a file ...
end;
except
// error ...
end;
或者,您可以直接使用Win32 API GetFileAttributes()
或GetFileAttributesEx()
函数,例如:
uses
...,Winapi.Windows;
var
attrs: DWORD;
begin
attrs := Windows.GetFileAttributes(PChar(path));
if attrs = INVALID_FILE_ATTRIBUTES then
begin
// error ...
end
else if (attrs and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
begin
// path is a folder ...
end else
begin
// path is a file ...
end;
end;
uses
...,Winapi.Windows;
var
data: WIN32_FILE_ATTRIBUTE_DATA;
begin
if not Windows.GetFileAttributesEx(PChar(path),GetFileExInfoStandard,@data) then
begin
// error ...
end
else if (data.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
begin
// path is a folder ...
end else
begin
// path is a file ...
end;
end;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。