更改Delphi OpenDialog的目录 [英] Changing the directory of Delphi OpenDialog
问题描述
程序的一点背景:
该程序使用一个标签式界面同时处理多个文件。
我正在尝试更改目录OpenDialog所以每次打开一个文件时都会显示目前我正在处理的文件目录,但是即使将 InitialDir
设置为文件的路径,它始终显示最后一个打开文件目录,而不是我设置的目录。
我尝试了以下内容:
A little background of the program:
The program uses a tabbed interface to work on multiple files at the same time.
I'm trying to change the directory of the OpenDialog so every time I call open a file will show the directory of the file I am working on currently, but even when I set InitialDir
to the file's path it always displays the last opened file directory, not the one I am setting it to.
I tried the following:
if Length(CurrentFileName) > 0 then
begin
OpenFileDialog.InitialDir :='';
SetCurrentDirectory(PChar(CurrentFileName));
OpenFileDialog.InitialDir := ExtractFileDir(CurrentFileName);
end;
if OpenFileDialog.Execute then
...
其中 CurrentFileName
是具有当前选项卡打开的文件的文件名的完整路径。但是没有运气。
Where CurrentFileName
is the full path with the filename of the current tab's opened file. But no luck.
有没有办法实现这个?
所以例如:
tab1
有 c:\mydir\file.txt
打开
tab2
有 d:\someotherdir\somefile.txt
打开
如果我移动到 tab1
并调用打开的函数我 OpenDialog
应该显示我的内容 c:\mydir\
If I move to tab1
and call the open function I the OpenDialog
should show me the contents of c:\mydir\
我正在使用Delphi 7.任何帮助
I am using Delphi 7. Any help is appreciated.
推荐答案
我以为
if Length(CurrentFileName) > 0 then
OpenFileDialog.FileName := ExtractFilePath(CurrentFileName);
if OpenFileDialog.Execute
是要走的路,但显然是
无论如何,我非常怀疑
OpenFileDialog.FileName := ExtractFilePath(CurrentFileName);
OpenFileDialog.InitialDir := OpenFileDialog.FileName;
SetCurrentDirectory(PChar(OpenFileDialog.FileName));
将使您失望。现在三个决定目标的主要方式就是说同样的事情!有点过度杀死,但如果Windows已经改变了它的行为,可能是必要的。
will make you disappointed. Now the three chief ways of determining the dir says the same thing! A bit over-kill, but if Windows has changed its behaviour, it might be necessary.
顺便说一下,你的代码中有一个错误。 SetCurrentDirectory
想要一个目录作为参数,而不是一个文件名。
By the way, there is a bug in your code. SetCurrentDirectory
wants a directory as argument, not a file name.
这篇关于更改Delphi OpenDialog的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!