请问,如何在选择文件对话框(最好是OpenDialog)中限制根目录?(100分)

B

balloy

Unregistered / Unconfirmed
GUEST, unregistred user!
比如限制根目录为: c:/temp,那,我就不让用户选择在此之外的目录,
但可以选择c:/temp的子目录。
就好像登陆ftp后,选择目录那样?

多谢大家!
 
DirectoryListBox可以啊
 
对不起,我刚才没说清楚,我是希望在OpenDialog中,限制根目录,我希望用户可以选中文件。

其实我的目的是这样的:我的程序中有一个导入本地Web站点的功能,
我现在的实现方法是这样的,
1。用SelectDirectory让用户选择一个目录,
2。弹出OpenDialog,让用户在他选择的根目录下选择一个文件,做为站点首页

第一步没什么问题,第二步时,我希望用户切换目录时,不能跳转到他选择的根目录外。
如果OpenDialog不能解决问题,使用Api可以的话,也没有关系,我想知道到底能不能做。

多谢大家!
 
像这面就可以:
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
if SelectDirectory('caption','c:/my documents',s) then
;
end;
 
DirectoryListBox+filelistbox完全可以实现啊
 
看看能不能在 OnFolderChange中做文章
另外使选择目录的组合框无效。

procedure TForm1.OpenDialog1FolderChange(Sender: TObject);
begin
OpenDialog1.filename:='d:/temp.txt';
showmessage(OpenDialog1.filename);
end;
 
uses FileCtrl;

var
sc: string;
begin
Sc := '';
if SelectDirectory('Select Directory', 'D:/Program Files/Borland', Sc) then
begin
caption := sc;
end;
end;
 
to jsxjd:
多谢,我也想过这种办法,但我如何改变dialog的当前目录呢?直接用FileName是不行的。
我在Delphi中查到:
The FileName property returns the name and complete directory path of the most recently selected file. The value of FileName is the same as the first item in the Files property.
To make a file name appear by default in the dialog’s edit box, assign a value to FileName in the Object Inspector or in program code. Programmatic changes to FileName have no effect while the dialog is active.
有什么其他办法吗?

to others:
对不起,一开始我没有把问题说清楚,我已经在三楼重新叙述了一下问题,多谢各位!
 
可以自己做一个窗体,用ListView(如果你用D6或D7的话好象还可以用ShellListView)显示文件
 
一个执衷的办法!

procedure TForm1.Button1Click(Sender: TObject);
var
s,sd:string;
begin
s:='d:/temp';
OpenDialog1.initialdir:=s;
if not(OpenDialog1.execute) then exit;
sd:=extractFileDir(OpenDialog1.FileName);
if AnsiCompareFileName(sd,s)<>0 then
begin
showmessage(Format('对不起,你只能在目录“%s”中选择文件。',));
Button1click(sender);
end;
// Ok;
end;
 
或者用TFileListBox !
 
Test OK for WinXP &amp; Win98:
Force Opendialog Only Open 'C:/';
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Button1: TButton;
Memo1: TMemo;
procedure OpenDialog1FolderChange(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Busy:Boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure SetOpenDialogDir(hWnd: HWND; Path: string;var Busy:Boolean);
const
ID_FILENAME_98 = 1152;
ID_FILENAME_WIN2K=1148;
ID_OK = 1;
var
hFileName: THandle;
hOK: THandle;
Buff:string[255];
begin
if Busy then exit;
Busy:=True;
if Win32MajorVersion >= 5 then
begin
hFileName:= GetDlgItem(GetParent(hWnd), ID_FILENAME_WIN2K);
hFileName:=FindWindowEx(hFileName,0,'ComboBox',nil);
hFileName:=FindWindowEx(hFileName,0,'Edit',nil);
end else hFileName := GetDlgItem(GetParent(hWnd), ID_FILENAME_98);
hOK := GetDlgItem(GetParent(hWnd), ID_OK);
GetWindowText(hFileName,@Buff[1],Length(Buff));
if LowerCase(Buff)=LowerCase(Path) then exit;
SetWindowText(hFileName, pchar(Path));
SendMessage(hOK, WM_LBUTTONDOWN, MK_LBUTTON, MAKEWORD(0, 0));
SendMessage(hOK, WM_LBUTTONUP, MK_LBUTTON, MAKEWORD(0, 0));
Busy :=False;
end;

procedure TForm1.OpenDialog1FolderChange(Sender: TObject);
begin
SetOpenDialogDir(OpenDialog1.Handle, 'C:/',Busy);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
end;
 
Kingron,
非常感谢你的回答,效果很好,但下面这句话:
if LowerCase(Buff)=LowerCase(Path) then exit;
我发现好像几乎起不了作用,我调试时,发现Buff值几乎总是乱七八糟的,所以,
目录不可能被改变,但我希望用户还可以选择根目录下的子目录,而不是完全不能改变目录。

所以,我写了:
if not IsOutSide( Path, Buff ) then ;exit //IsOutSide是我另外写的函数,判断一特定目录是否在根目录下
但由于Buff值的不确定性,不能达到目标,请问如何解决?(我的操作系统是Win2k Pro)
多谢!
 
if Pos('C:/',OpenDialog1.FileName)=0 then
SetOpenDialogDir(OpenDialog1.Handle, 'C:/',Busy);
去掉那个 GetWindowText(hFileName,@Buff[1],Length(Buff));
if LowerCase(Buff)=LowerCase(Path) then exit;
我很忙,其他的你自己去做吧,有个小BUG
 
是啊,在OnFolderChange事件中,
OpenDialog1.FileName也不准啊,连续按几次“向上一级”按钮,就会出错了,我试着去
读/写 那个ComboBox的值,但也没有效果,怎么做啊?

谢谢大家了!
 
如果让组合框不可选择能满足你要求吗,如果行的话,我试试。
 
to jsxjd:
真是感谢你的热心帮助,你又提供了解决这个问题的一种新思路,非常感谢!
不过,如果仅仅让组合框不可选还不够,用户多次点击“向上一级”按钮呢?
 
你把那个Combobox禁止掉不久可以了?

如果在2K/XP下面,可以用SelectDirectory来代替!就是那个标准的浏览目录对话框,
这个对话框在2K/XP下支持选择文件!!
 
此外,你也可以在OpenDialog的CanClose中添加代码阿!这样一定可以限制阿勒。
 
多人接受答案了。
 
顶部