kimifdw的专栏

随时记录在项目开发中遇到的问题 也欢迎大家纠正

Delphi学习笔记六——文件路径相关的字符串操作

uses
  HTTPApp, Masks;

procedure TForm1.Button1Click(Sender: TObject);
var
  ss,s: string;
begin
  //先提取一个文件名的字符串
  ss := Application.ExeName;
  ShowMessage(ss); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.exe

  //路径
  s := ExtractFilePath(ss);
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\

  //所在文件夹
  s := ExtractFileDir(ss);
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects

  //所在逻辑盘
  s := ExtractFileDrive(ss);
  ShowMessage(s); //C:

  //文件名
  s := ExtractFileName(ss);
  ShowMessage(s); //Project1.exe

  //文件扩展名
  s := ExtractFileExt(ss);
  ShowMessage(s); //.exe

  //相对路径
  s := ExtractRelativePath('C:\Documents and Settings\wy\My Documents\',ss);
  ShowMessage(s); //RAD Studio\Projects\Project1.exe

  //dos下的短文件名
  s := ExtractShortPathName(ss);
  ShowMessage(s); //C:\DOCUME~1\wy\MYDOCU~1\RADSTU~1\Projects\Project1.exe

  //转换到Unix路径格式, 需要 uses HTTPApp
  s := DosPathToUnixPath(ss);
  ShowMessage(s); //C:/Documents and Settings/wy/My Documents/RAD Studio/Projects/Project1.exe

  //转Unix路径格式为Window格式, 是 DosPathToUnixPath 的逆操作
  s := ExpandUNCFileName(DosPathToUnixPath(ss));
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.exe

  //去掉结尾的路径分隔符
  s := ExcludeTrailingPathDelimiter(ExtractFilePath(ss));
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects

  //去掉结尾的路径分隔符, 只是调用:ExcludeTrailingPathDelimiter
  s := ExcludeTrailingBackslash(ExtractFilePath(ss));
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects

  //更换扩展名, 并不修改文件, 只是替换字符串:
  s := ChangeFileExt(ss,'.bak');
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.bak

  //更换文件路径:
  s := ChangeFilePath(ss,'c:\temp');
  ShowMessage(s); //c:\temp\Project1.exe

  //判断第几个字符是否是路径分隔符:
  IsPathDelimiter(ss,3); //返回 True

  //判断后缀名是不是 exe; 需要 uses Masks
  MatchesMask(ss,'*.exe'); //返回 True
end;
--------------------------------------------------------------------------------

//分解 ProcessPath (需要 uses FileCtrl)
var
  s,p,f: string;
  d: Char;
begin
  s := ParamStr(0);
  ShowMessage(s); //C:\Documents and Settings\wy\My Documents\RAD Studio\Projects\Project1.exe
  ProcessPath(s,d,p,f);
  ShowMessage(d); //C
  ShowMessage(p); //\Documents and Settings\wy\My Documents\RAD Studio\Projects
  ShowMessage(f); //Project1.exe
end;
--------------------------------------------------------------------------------

//压缩显示 MinimizeName (需要 uses FileCtrl)
var
  s: string;
begin
  s := ParamStr(0);
  s := MinimizeName(s,Self.Canvas,100);
  ShowMessage(s); //C:\...\Project1.exe
end;
万老师博客上转载的,很不错的整理
阅读更多
个人分类: Delphi
所属专栏: Delphi开发学习笔记
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭