怎么样确定一个文件所在的盘符? (100分)

  • 主题发起人 bianconeri
  • 开始时间
B

bianconeri

Unregistered / Unconfirmed
GUEST, unregistred user!
不能确定一个文件将存放的盘符,怎么通过程序确定其所在盘符。否则程序找不到此文件
话,将不能正确运行。我现在必须把此文件放在最后一个驱动器才可以正常运行。如果换
一个驱动器就不行了。
因为程序在别人机器上可能不会放置在最后一个驱动器,所以需要这样一段代码确定其所
在盘符。

望达人指点!
 
在线等待!
 

那个文件可以考虑放在应用程序的目录下或者系统目录下。
也可以考虑在程序运行时,if not FileExists then 给一个OpenDialog,要求指定文件位置。
 
用ExtractFileDir(Application.ExeName)将得到应用程序的当前路径。
例如你的某个文件放在应用程序的aaa目录下,只须用下面一行代码那可正常调用:
ExtractFileDir(Application.ExeName)+'/'+'aaa'+'你的文件名'

 
我主要是确定其所在盘符,或者这样说吧

遍历从C到Z盘,判断那个盘下面存放了我所要的文件!
 

找一些遍历目录,查找文件的帖子看看
 
用path := Applicaton.ExeName可以得到你的程序的文件名,然后 path[1]不就是盘符名
吗?
比如 path := Applicaton.ExeName;//path := 'D:/abc/abc.txt';
path[1] := 'D';
你可以把你要的文件和你的程序放在同一个路径就可以了。
 
先获得文件路径(用ExtractFilePath函数),然后再截取前两个字符不就行了吗,楼上的方法也是好方法
Drivce:=copy(ExtractFilePath(Filename),1,2)
 
ExtractFileDrive !!!!!!!!


Returns the drive portion of a file name.

Unit

Sysutils

Category

file name utilities

function ExtractFileDrive(const FileName: string): string;

Description

ExtractFileDrive returns a string containing the drive portion of a fully qualified path name for the file passed in the FileName. For file names with drive letters, the result is in the form '<drive>'. For file names with a UNC path the result is in the form '//<servername>/<sharename>'. If the given path contains neither style of path prefix, the result is an empty string.
 
FindFile API
 
function ExtractFileDrive(const FileName: string): string;

多看帮助,老兄!
 
顶部