如何获取某个目录的父文件夹的路径? [英] How to get path to the parent folder of a certain directory?

查看:386
本文介绍了如何获取某个目录的父文件夹的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有这个目录路径:

  C:\Program Files(x86 )\Embarcadero\ 

我正在寻找的函数应该返回:

  C:\程序文件(x86)\ 
pre>

我已经尝试过这个代码,但它只适用于文件,而不适用于目录:

  function GetParentDirectory(const Path:string):string; 
begin
结果:= ExpandFileName(Path);
结束有没有人知道我应该使用什么来将路径返回到某个目录的父文件夹?


解决方案

您可以使用以下几种方式:



从文件夹名称:

  ExtractFilePath(ExcludeTrailingPathDelimiter('C:\Parent\Child\')); 

从文件名称:

  ExtractFilePath(ExcludeTrailingPathDelimiter(ExtractFilePath('C:\Parent\Child\app.exe'))); 


When I would have this directory path:

C:\Program Files (x86)\Embarcadero\

The function I'm looking for should return:

C:\Program Files (x86)\

I've tried this code but it works only for files, not for directories:

function GetParentDirectory(const Path: string): string;
begin
  Result := ExpandFileName(Path);
end;

Does anyone know what should I use to return path to the parent folder of a certain directory ?

解决方案

You can use a couple of ways:

From a folder name:

ExtractFilePath(ExcludeTrailingPathDelimiter('C:\Parent\Child\'));

From a file name:

ExtractFilePath(ExcludeTrailingPathDelimiter(ExtractFilePath('C:\Parent\Child\app.exe')));

这篇关于如何获取某个目录的父文件夹的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆