用delphi 判断系统根目录

用delphi 判断系统根目录,第1张

//系统目录

function GetSystemDir: string

var

Buf: array[0..MAX_PATH] of char

begin

GetSystemDirectory(Buf, MAX_PATH)

Result := Buf

if Result[Length(Result)] <>'\' then

Result := Result + '\'

end

这个函数返回值的前三个字母就是系统根目录

我现在很少用C,不过GetSystemDirectory是系统API函数,应该也可以用的,只是要注意参数的定义

给一个通用过程,直接调用,运行看是不是你想要的效果。

procedure GetChildFileList(AStrings: TStrings ASourFile,

    FileName: string) // 查找子目录

 // AStrings存放路径, ASourceFile要查找的目录,FileName搜索的文件类型 若指定类型,则'*.jpg'or '*.png'

var

  sour_path, sour_file: string

  TmpList: TStringList

  FileRec, subFileRec: TSearchrec

  i: Integer

begin

    if copy(ASourFile, Length(ASourFile), 1) <> '\' then

    sour_path := IncludeTrailingPathDelimiter(Trim(ASourFile))      // 在路径后面加上反斜杠

  else

    sour_path := trim(ASourFile)

    sour_file := FileName

  if not DirectoryExists(sour_path) then

  begin

    AStrings.Clear

    exit

    end

  TmpList := TStringList.Create

  TmpList.Clear

  if FindFirst(sour_path + '*.*', faAnyfile, FileRec) = 0 then

    repeat

    if ((FileRec.Attr and faDirectory) <> 0) then

    begin

      if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then

      GetChildFileList(AStrings, sour_path + FileRec.Name + '\', sour_file)

    end

  until FindNext(FileRec) <> 0

  FindClose(FileRec)

  if FindFirst(sour_path + FileName, faAnyfile, subFileRec) = 0 then

  repeat

    if ((subFileRec.Attr and faDirectory) = 0) then

    TmpList.Add(sour_path + subFileRec.Name)

  until FindNext(subFileRec) <> 0

    FindClose(subFileRec)

  for i := 0 to TmpList.Count - 1 do

    AStrings.Add(TmpList.Strings[i])

    TmpList.Free

end

调用:

procedure TForm2.SpeedButton5Click(Sender: TObject)

begin

    GetChildFileList(ListBox1.Items, 'D:\Wyp\', '*.jpg')  // 目录自己定  

    GetChildFileList(ListBox1.Items, 'D:\Wyp\', '*.png')    

end

这里是将查找的目录存放在ListBox里的。

在加载List时,由于Item太多,所以有一定的延时,而不是卡死。

希望能帮到你。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12050370.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-20
下一篇2023-05-20

发表评论

登录后才能评论

评论列表(0条)

    保存