• MaxVoit (19.10.18 18:00) [0]
    Добрый день, такая проблема.
    Нужно открывать каталог с exe по умолчанию.
    Но всегда открывает последний каталог из которого выбирал файл.
    Прошерстил всё по сабжу, вроде всё просто, но ничего не помогает.

    procedure TForm1.Button1Click(Sender: TObject);
    begin
         OpenDialog1.Filename:='';
         OpenDialog1.InitialDir:=ExtractFilePath(ParamStr(0));
         //OpenDialog1.InitialDir:=GetCurrentDir;
         if OpenDialog1.Execute then ...

    В свойствах ofNoChangeDir стоит true.

    Win7
  • Leonid Troyanovsky © (23.10.18 10:49) [1]

    > MaxVoit   (19.10.18 18:00) 

    > Нужно открывать каталог с exe по умолчанию.

    C InitialDir был давний косяк. А может это и фича.

    Если хочется больше контроля, то надо браться за winapi.
    Примерно так:

    uses
     commdlg;

    procedure TForm1.Button1Click(Sender: TObject);
    var
     ofs : TOpenFilename;
     files : array [0..$FFF] of char;
    begin
     with ofs do
       begin
         FillChar(ofs, sizeof(ofs), 0);
         FillChar(files, sizeof(files),0);
         lStructSize := sizeof(ofs);
         hWndOwner := GetForegroundWindow;
         lpstrFilter := 'txt files (*.txt)'#0'*.txt'#0#0;
         nFilterIndex:= 1;
         lpstrFile := files;
         nMaxFile := SizeOf(files)-1;
         lpstrInitialDir := PChar(ExtractFilePath(ParamStr(0)));  // вот оно
         Flags := OFN_ALLOWMULTISELECT or
                  OFN_FILEMUSTEXIST or
                  OFN_EXPLORER;
         lpstrTitle := 'Open my text files';
         if GetOpenFileName(ofs) then
           {..};
       end;
    end;

    Работа над ошибками оставлена в качестве задания на дом.
    --
    Regards, LVT.
  • Styx © (23.10.18 22:57) [2]

    > C InitialDir был давний косяк. А может это и фича.
    > Если хочется больше контроля, то надо браться за winapi.

    Не поможет, потому что этот косяк/фича в винапи и живут:
    https://docs.microsoft.com/ru-ru/windows/desktop/api/commdlg/ns-commdlg-tagofna
    The initial directory. The algorithm for selecting the initial directory varies on different platforms.

    Windows 7:

    If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.
    Otherwise, if lpstrFile contains a path, that path is the initial directory.
    Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory.
    If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
    Otherwise, the initial directory is the personal files directory of the current user.
    Otherwise, the initial directory is the Desktop folder.
    Windows 2000/XP/Vista:
    If lpstrFile contains a path, that path is the initial directory.
    Otherwise, lpstrInitialDir specifies the initial directory.
    Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded.
    If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
    Otherwise, the initial directory is the personal files directory of the current user.
    Otherwise, the initial directory is the Desktop folder.
  • Leonid Troyanovsky © (24.10.18 10:32) [3]

    > Styx ©   (23.10.18 22:57) [2]

    > Не поможет, потому что этот косяк/фича в винапи и живут:

    В Win7 код работает как хотел ТС.

    --
    Regards, LVT.
  • sniknik © (25.10.18 08:37) [4]
    проверил, оба варианта работают и под семеркой, и под десяткой.
    только на десятке от параметра ofNoChangeDir в коде автора ничего не зависит работает, как он хочет, как ни установлен, а на семерке только при fNoChangeDir=true.

    MaxVoit
    проверь приведенный тобой код на пустом проекте, в твоем видимо что то мешает.
  • sniknik © (25.10.18 08:39) [5]
    и да вот без этого OpenDialog1.Filename:= ''; все не так, если есть выбранный уже файл, то его путь в приоритете...
  • Leonid Troyanovsky © (25.10.18 13:43) [6]

    sniknik ©   (25.10.18 08:37) [4]

    > только на десятке от параметра ofNoChangeDir в коде автора
    > ничего не зависит работает, как он хочет, как ни установлен,
    >  а на семерке только при fNoChangeDir=true.

    Проверил под семеркой код автора - работает как он хочет
    вне зависимости от установки fNoChangeDir.
    Который флаг, действительно, меняет/не меняет CurrentDir.

    --
    Regards, LVT.
  • Leonid Troyanovsky © (25.10.18 14:00) [7]

    > sniknik ©   (25.10.18 08:39) [5]

    >  если есть выбранный уже файл, то его путь в приоритете.

    Ну, как отмечено в [3], оно как-бы обещано:
    if lpstrFile contains a path, that path is the initial directory.

    А, во-ще-то, диалоги круто замешаны, сам черт ногу сломит.
    Не даром их обслуживают несколько потоков.

    М.б. для исходной задачи хватило и листбокса with LB_DIR.

    --
    Regards, LVT.
Есть новые Нет новых   [118451   +42][b:0][p:0]