Delphi 获取指定文件的驱动器名, 文件夹名, 路径名, 文件名

复制代码
var
  fullFileName : string;

begin
  // Set up a full file name with drive and path
  fullFileName := 'C:\Program Files\Borland\Delphi7\Projects\Unit1.dcu';

  // Show the component parts of this full name
  ShowMessage('Drive = '+ExtractFileDrive (fullFileName));
  ShowMessage('Dir   = '+ExtractFileDir   (fullFileName));
  ShowMessage('Path  = '+ExtractFilePath  (fullFileName));
  ShowMessage('Name  = '+ExtractFileName  (fullFileName));
  ShowMessage('Ext   = '+ExtractFileExt   (fullFileName));
end;
复制代码
Drive = C:
Dir   = C:\Program Files\Borland\Delphi7\Projects
Path  = C:\Program Files\Borland\Delphi7\Projects\
Name  = Unit1.dcu
Ext   = .dcu

//获取应程序文件的路径
ExtractFilePath(Application.ExeName);

复制代码
var
  oldName, newName : string;

begin
  // Try to rename the current Unit1.dcu to Uni1.old
  oldName := 'Unit1.dcu';
  newName := ChangeFileExt(oldName, '.new');

  // Show the old and new values
  ShowMessage('Old name = '+oldName);
  ShowMessage('New name = '+newName);
end;
复制代码
Old name = Unit1.dcu
New name = Unit1.new
复制代码
var
  selectedFile   : string;
  drive          : char;
  path, fileName : string;

begin
  // Ask the user to select a file
  if PromptForFileName(selectedFile)
  then
  begin
    // Display this full file/path value
    ShowMessage('Selected file = '+selectedFile);

    // Split this full file/path value into its constituent parts
    ProcessPath(selectedFile, drive, path, fileName);
    ShowMessage('drive = '+drive);
    ShowMessage('path = '+path);
    ShowMessage('fileName = '+fileName);
  end;
end;
复制代码
 { The user selects C:\Files\data.txt and hits OK }
   
   Selected file = C:\Files\data.txt
   drive = C
   path = \Files
   filename = data.txt

http://www.delphibasics.co.uk/index.html

 

 

posted @   IAmAProgrammer  阅读(922)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示