Am using GetProgrammDir to get my application folder and it works but I need more control over it were I can select folders inside or outside of my program say there 2 folders Data and Items data will store exe and items will store information txt but the data will be out side of the data folder like below

C:\MyProgra\Items\Readme.txt
C:\MyProgram\Data\RunMe.exe

Now when I use GetDir I get data and exe folder fine but am unable to modify it to get MyProgram Folder only so that I can set the needed folders like

C:\MyProgram\<THEN I WOULD SET IT HERE IN DELPHI>

anyone know how to do this?, Thanks

Recommended Answers

All 3 Replies

Windows applications should be using:

GetSpecialFolderLocation(CSIDL_APPDATA);

to store application data in the current user's folder.

Thanks bud, but that only seams to show

c:\Users\Simon\Desktop\

am after away of getting my program directory but not the folder were the exe is

currently getdir shows as

c:\Users\Simon\AppData\Roaming\MyProgram\System\My.exe

but I need away of just getting the programs location like

c:\Users\Simon\AppData\Roaming\MyProgram\

from the exe so that if I install program to a other location it will still work.

Study this code:

var
  EXEName,
  EXEFolder,
  ParentFolder,
  DataFolder : string;
begin
  Memo1.Clear;
  EXEName := Application.ExeName;
  EXEFolder := ExtractFilePath(ExeName);
  ParentFolder := ExtractFilePath(ExcludeTrailingPathDelimiter(EXEFolder));
  DataFolder := IncludeTrailingPathDelimiter(ParentFolder) + 'Data';

However, although this should answer the question you asked, it doesn't answer the question you should have asked. The point pritaeas made is that your app should not be using that directory structure. Instead it should store application data in the folder returned by:

GetSpecialFolderLocation(CSIDL_APPDATA);

This will not return the folder you are actually using, because your app's data is in the wrong place. It will return the folder where you really should put the data. Having said that, this approach is now out of date and for new code you should use ShGetKnownFolderPath and FOLDERID_RoamingAppData but this whole area is a real mess. The API has changed several times and it isn't an easy task to get it right if you need to run on a variety of Windows versions. Also I think the VCL doesn't import ShGetKnownFolderPath for you so you would need to do that (and define GUIDs for the FOLDERID constants) yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.