如何得到一个程序的绝对路径? function ExtractFilePath(const FileName: string): string; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用function ExtractFilePath(const FileName: string): string;例:procedure TForm1.Save1Click(Sender: TObject);var NewFileName: string; Msg: string; NewFile: TFileStream; OldFile: TFileStream;begin NewFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit1.Text); Msg := Format('Copy %s to %s?', [Edit1.Text, NewFileName]); if MessageDlg(Msg, mtConfirmation, mbOKCancel, 0) = mrOK then begin OldFile := TFileStream.Create(Edit1.Text, fmOpenRead or fmShareDenyWrite); try NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyRead); try NewFile.CopyFrom(OldFile, OldFile.Size); finally FreeAndNil(NewFile); end; finally FreeAndNil(OldFile); end; end; 1.本程序:application.exename:string;2.其它程序:function ExtractFilePath(const FileName: string): string; 我试过了,用ExtractFilePath不行。例如,硬盘上有个程序文件:c:\test\p1.exe(它并没有运行),用ExtractFilePath('p1.exe')就能返回c:\test吗?不行吧? ExtractFilePath函数只是简单的从FileName参数中取出路径,而不是查找文件所在的目录。 可以读注册表,只要这个程序通过按装程序安装而且那个安装程序没有BUG.比如程序A.exeHKEY_LOCAL_MACHINE\Softare\Microsoft\Windows\CurrentVersion\App Paths\A.exe,在下面读Path值,这就是A.exe的绝对路径. ExtractFilePath就是可以找到你的文件的路径啊,这一点没错。没运行也可以。 to Liujc(阿聪):大虾所言极是。我也这么认为。因为我用ExtractFilePath('p1.exe')返回的是'',而ExtractFilePath('c:\test\p1.exe'),返回的才是c:\test\,哈哈,既然需事先知道,又何必用它呢?我觉得这不是用一个简单的函数就能搞定的吧?其实这是一个文件查找问题。请大家帮忙呀!to BCB_FANS(四大名捕之追杀令) :问题是怎样在注册表中快速找到这一项呢?请继续指教。 學習:有另問題:如果A.exe存在兩個以上的路徑下,我們得到的應該是甚麼呢 我不是已经说定很明白了,你要找的程序你总知道它的名城吧???HKEY_LOCAL_MACHINE\Softare\Microsoft\Windows\CurrentVersion\App Paths\要找的程序的名城(包括扩展名),在这个键底下读Path这个字符串值,就是路径啊!!! 你想要的是一个搜索文件的过程.自编一个对硬盘进行Tree遍历的方法去实现,象Windows中的搜索文件一样. BCB_FANS(四大名捕之追杀令):谢谢。你的办法较好。to gxyc76(dd) 若用遍历的办法,可能时间很长,(用过windows的查找功能的都有这种体会) 请问下应用系统正在做处理时,如何控制不能做其他的操作 delphi format 显示精度问题 EnTest.dll 是什么 失意中~不知道该怎么办!大家给点意见吧~ Delphi 7 下FTP控件的一个BUG. 如何设置DBGrid的行高 偶不知道為何總是出現錯誤'I/O error 32',是什麽原因? 学习Delphi数据库, 哪本书好? SQL呢? 关于多窗体的一些问题? MDI应用程序中子窗口最大化打开时,会出现闪烁——为什么??? 为什么我用adodataset1.Append;添加数据,如果在有D的电脑上就没事,但到别的电脑上就说找不到ADO对象?? 信誉分为0会怎样?
procedure TForm1.Save1Click(Sender: TObject);var
NewFileName: string;
Msg: string;
NewFile: TFileStream;
OldFile: TFileStream;
begin
NewFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit1.Text);
Msg := Format('Copy %s to %s?', [Edit1.Text, NewFileName]);
if MessageDlg(Msg, mtConfirmation, mbOKCancel, 0) = mrOK then
begin
OldFile := TFileStream.Create(Edit1.Text, fmOpenRead or fmShareDenyWrite);
try
NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyRead); try
NewFile.CopyFrom(OldFile, OldFile.Size);
finally
FreeAndNil(NewFile);
end;
finally
FreeAndNil(OldFile);
end;
end;
2.其它程序:function ExtractFilePath(const FileName: string): string;
c:\test\p1.exe(它并没有运行),用ExtractFilePath('p1.exe')就能返回
c:\test吗?不行吧?
\App Paths\A.exe,在下面读Path值,这就是A.exe的绝对路径.
没运行也可以。
大虾所言极是。我也这么认为。因为我用ExtractFilePath('p1.exe')返回的是'',而ExtractFilePath('c:\test\p1.exe'),返回的才是c:\test\,哈哈,既然需事先知道,又何必用它呢?我觉得这不是用一个简单的函数就能搞定的吧?其实这是一个文件查找问题。
请大家帮忙呀!to BCB_FANS(四大名捕之追杀令) :
问题是怎样在注册表中快速找到这一项呢?请继续指教。
有另問題:如果A.exe存在兩個以上的路徑下,我們得到的應該是甚麼呢
\App Paths\要找的程序的名城(包括扩展名),在这个键底下读Path这个字符串值,就是路径啊!!!
自编一个对硬盘进行Tree遍历的方法去实现,象Windows中的搜索文件一样.
谢谢。你的办法较好。to gxyc76(dd)
若用遍历的办法,可能时间很长,(用过windows的查找功能的都有这种体会)