News:

Download Pelles C here: http://www.pellesc.se

Main Menu

Working Directory woes

Started by PhilG57, July 02, 2026, 04:16:44 PM

Previous topic - Next topic

PhilG57

I have a Windows project with multiple versions kept in separate directories.  I edit and update in one directory and when those changes look and test good, I move them to another, separate, directory of more stable code.

In any of those project versions, when I open a file with GetOpenFileName(), the directory shown from which to select a file to open, is the directory last used by Pelles C.  For example, in the GetOpenFileName() dialog, if I change to a different directory and select a file from there, that directory becomes the 'default' location shown by GetOpenFileName() the next time it is called.

So far, so good I think.  What I don't understand is with the opening of a new or different project using the same instance of Pelles C, or especially with a new clean instance of Pelles C, why does the default GetOpenFileName() use what I selected before and not the default directory listed in my project's options. As part of the initialization code, I do execute a GetCurrentDirectory() followed by a SetCurrentDirectory() to no avail.

As always, thanks in advance.

PhilG57

Stack Overflow says the directory last selected by GetOpenFileName is stored in the registry and used the next time GetOpenFileName is called.  This seems to be the case, even though I make a point to reset the working directory to that obtained earlier in my code.  As also suggested there, I added the flag "OFN_NOCHANGEDIR" to the GetOpenFileName call with no change in behavior.

I do think this is a user caused error but have yet to determine where I went wrong.

John Z

Hi PhilG57,

Have you tried setting the ofn.lpstrFile parameter to the path you want to start in?
I do this, it seems to work.  Documentation says this will be used as the initial directory when the dialog starts, unless the value is NULL.  Also may need to set lpstrInitialDir to NULL, but I don't recall doing that - ?

The file name used to initialize the File Name edit control.
The first character of this buffer must be NULL if initialization
is not necessary. When the GetOpenFileName or GetSaveFileName
function returns successfully, this buffer contains the drive
designator, path, file name, and extension of the selected file.

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.

Windows 2000/XP/Vista:

    If lpstrFile contains a path, that path is the initial directory.
    Otherwise, lpstrInitialDir specifies the initial directory.

https://learn.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-openfilenamea


John Z