【问题标题】:GetDir in Delphi2010 not working under Windows 7?Delphi2010 中的GetDir 不能在Windows 7 下工作?
【发布时间】:2011-04-15 15:02:45
【问题描述】:

我在 Delphi2010 中有以下命令序列:

  var netdir:string;
  ....
  OpenDialog1.InitialDir:=netdir;
  ....
  OpenDialog1.Execute...
  ....
  GetDir(0,netdir);
  ....

执行 OpenDialog 后,我应该在字符串 netdir 中包含我完成的目录 我的 OpenDialog.Execute。在下一个 OpenDialog.Execute 它应该从那个开始 目录。 它在 XP 上运行良好,但在 Windows 7 上不行? 它总是从安装程序的目录开始。

知道可能出了什么问题吗?

谢谢。

【问题讨论】:

    标签: delphi windows-7 delphi-2010 topendialog opendialog


    【解决方案1】:

    您的问题无法按现状回答,因为它缺少几个关键细节。

    1. netdir 是全局常量,还是时不时超出范围?
    2. 您是否将netdir 设置为OpenDialog1.Execute 之前的某个值?
    3. 问题是关于GetDir 返回哪个目录(如您的标题所示),还是关于如何使打开的对话框记住上次访问的目录(如正文所示)?

    我会假设 1) netdir 是一个全局常量,2) 您最初没有设置它,并且 3) 您希望打开的对话框记住上次访问的文件夹。因此你有类似的东西

    unit Unit3;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;
    
    type
      TForm3 = class(TForm)
        OpenDialog1: TOpenDialog;
        procedure FormClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form3: TForm3;
    
    var
      netdir: string;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm3.FormClick(Sender: TObject);
    begin
      OpenDialog1.InitialDir := netdir;
      OpenDialog1.Execute;
      GetDir(0, netdir);
    end;
    
    end.
    

    那么解决办法就是让windows为你记住目录,也就是干脆做

    procedure TForm3.FormClick(Sender: TObject);
    begin
      OpenDialog1.Execute;
    end;
    

    一个人!但是为什么你的方法不起作用?好吧,GetDir 不会返回你想要的。如果您需要显式控制,请执行

    procedure TForm3.FormClick(Sender: TObject);
    begin
      OpenDialog1.InitialDir := netdir;
      OpenDialog1.Execute;
      netdir := ExtractFilePath(OpenDialog1.FileName)
    end;
    

    【讨论】:

      【解决方案2】:

      如果您不想打开对话框,您可以执行以下操作来获取程序下的目录。

      yourdir:=ExtractFilePath(Application.ExeName);
      

      我已经在 Vista 中完成了它,它可以工作。

      【讨论】:

        【解决方案3】:

        这就是问题的解决方案

        openDialog1.Options := [ofFileMustExist];
        
        if openDialog1.Execute then
        begin
        
        end;
        

        【讨论】:

          猜你喜欢
          • 2020-03-11
          • 1970-01-01
          • 2014-08-01
          • 2013-07-28
          • 2019-02-09
          • 1970-01-01
          • 2011-03-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多