根据下面我总结出两个方法经常用到:
ExtractFilePath(paramstr(0));
ExtractFilePath(Application.ExeName);
输出的都是://C:\Documents and Settings\wy\My Documents\RAD Studio\Projects
-----------------------------------------------------------------------------------------------
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;
procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(ExtractFilePath(ParamStr(0))); //获取程序所在目录 ShowMessage(ExtractFilePath(Application.ExeName)); //获取程序所在目录 ShowMessage(GetCurrentDir + '\'); //获取当前目录{以后用这个就可以了,上面的两个都可能有点慢,因为都经过了两次转换,里面的代码也很多,从名字上判断第三个是专一函数,} end;
以后就用第三个了,三个运行出来的结果都一样,但是第三个从名字上判断是专一的函数,前两个 都是 方法中 再次嵌套方法,会造成速度变慢。再加上前两个的主要也不仅仅是程序目录的,前两个 功能很强大应该
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/archive/2011/12/27/2303770.html