TDirectory.GetParent获取指定目录的父目录

使用函数:

  System.IOUtils.TDirectory.GetParent

class function GetParent(const Path: string): string; static;

说明:返回给定目录的父目录,相对路径则为当前工作路径

异常处理:指定目录不存在或无效

代码:

procedure TForm1.Button1Click(Sender: TObject);
begin
    try
        //获取指定目录的父目录
        //异常:指定目录不存在或无效
        ShowMessage(Edit1.Text + '的父目录为:' + TDirectory.GetParent(Trim(Edit1.Text)));
    except
        on e: Exception do
        begin
            MessageDlg(e.ClassName + ' : ' + e.Message, mtError, [mbok], 0);
            Exit;
        end;
    end;
end;

原文地址:https://www.cnblogs.com/cause/p/3501841.html