3

I need to read and write a configuration file to windows disk in WinXP through Win8. Where is they best place to do this? Doesn't seem like the ProgramData folder is allowing

procedure TfrmMain.FormCreate(Sender: TObject);
var
  path: array[0..MAX_PATH] of char;
begin
   SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, SHGFP_TYPE_CURRENT, @path);
  AppPath:= Path;
  AppPath:= AppPath + '\Customer\';
  if not DirectoryExists(AppPath) then
   CreateDir(AppPath);
 if FileExists(AppPath + 'Customers.cst') then
 LoadFromFile(AppPath + 'Customers.cst');
end;

procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
 if ListModified  then
 begin
  if MessageDlg('Save Changes?', mtWarning, [mbYes, mbNo], 0) = mrYes  then
   SaveToFile(AppPath + 'Customers.cst');
  canClose:= True;
 end
 else 
canClose:= False;
end;
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
JakeSays
  • 2,048
  • 8
  • 29
  • 43

1 Answers1

10

CSIDL_COMMON_APPDATA is described as:

The file system directory that contains application data for all users.

Because it is shared between all users on the computer, you need to have admin rights to write to that location. If you want your configuration to be shared by all users then CSIDL_COMMON_APPDATA is the right place for it. However, you'll need to make sure that you have sufficient rights when you come to write there.

If you do need your application to write to CSIDL_COMMON_APPDATA then the normal approach is to create a directory for your application during installation. Because your installer will run elevated it can do this. It must also add a permissive ACL to the new directory so that your application can later, when running as standard user, write to that folder.

If you want a configuration that is stored in the user profile then you should choose a location under CSIDL_APPDATA, described as:

The file system directory that serves as a common repository for application-specific data.

Because this is in the user profile, each user on the machine will have a separate copy of the configuration file.

kobik
  • 21,001
  • 4
  • 61
  • 121
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490