opendialog1.exceute是需要用户选中了文件夹中具体的文件(如:temp.txt)才为true,而且返回的opendialog1.filename是完整路径名(如c:\test\temp.txt)但是现在我只想得到文件夹的路径而已(也就是:c:\test),所以我希望用户只要在opendialog中选择到了test这个文件夹,就可以按"确定"返回这个(c:\test)路径名。而不用选择具体的文件(如temp.txt)。因为文件夹中可能根本没有文件。这个应该怎么做呢!help!!谢谢

解决方案 »

  1.   

    This example uses a button and a label on a form. When the user clicks the button, a Select Directory dialog box appears. The current directory displayed in the dialog box is C:\MYDIR. The user can select a directory from the directory list, or enter a new directory in the edit box. If the user enters a new directory, a message box asks the user if the directory should be created. If the user chooses Yes, the directory is created. If the user chooses No, the message box goes away without creating the directory. The name of the directory the user selects appears as the caption of the label:#include <FileCtrl.hpp>
    const SELDIRHELP = 1000;
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
      AnsiString Dir = "C:\\Program Files\\MyApp";
      if (SelectDirectory(Dir, TSelectDirOpts() << sdAllowCreate << sdPerformCreate << sdPrompt,SELDIRHELP))
        Label1->Caption = Dir;
    }
      

  2.   

    This example uses a button and a label on a form. When the user clicks the button, a Select Directory dialog box appears. The current directory displayed in the dialog box is C:\MYDIR. The user can select a directory from the directory list, or enter a new directory in the edit box. If the user enters a new directory, a message box asks the user if the directory should be created. If the user chooses Yes, the directory is created. If the user chooses No, the message box goes away without creating the directory. The name of the directory the user selects appears as the caption of the label:uses FileCtrl;const
      SELDIRHELP = 1000;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Dir: string;
    begin
      Dir := 'C:\MYDIR';
      if SelectDirectory(Dir, [sdAllowCreate, sdPerformCreate, sdPrompt],SELDIRHELP) then
        Label1.Caption := Dir;
    end;
      

  3.   

    谢谢,可是为什么会有一个错误说:
    [Warning] unifrmMain.pas(8): Unit 'FileCtrl' is specific to a platform
      

  4.   

    因为你要的是目录,可以个OpenDialog一个默认文件名;如
    OpenDialog1.FileName:='选择文件';
    if Opendialog1.Execute then
    ShowMessage(ExtractFilePath(Opendialog1.FileName));
      

  5.   

    [Warning] unifrmMain.pas(8): Unit 'FileCtrl' is specific to a platform
    只是一个警告,说filectrl是个平台有关,不同的平台可能出现错误。可以不用管它。
    --------------------------------------------------------------------
    看尽悲伤,庸人自扰不平事。叹尽荒凉,海阔天空谁人知。狂风劲兮,百花飘
    扬乱舞香。捏花一笑,海不扬波断肠心!
      

  6.   

    最后我采取了rockswj(石头)的方法,outer2000(天外流星) 的方法最方便,不过外观不太好。石头的方法因为会选中文件的(即使是虚的文件,如FileName:='选择文件'),所以最后还是要截取一下filename来得到路径:
    FilePath := leftstr(FileName,length(FileName)-pos('\',reversestring(FileName))+1);