0

I get the common app data folder to store my application related files. Eg log files and ini config files. I use the following code:

const
    CSIDL_COMMON_APPDATA     = $0023;

function TSpecialFolders.GetSpecialFolder(
  const ASpecialFolderID: Integer): string;
var
   vSFolder :  pItemIDList;
  vSpecialPath : array[0..MAX_PATH] of Char;
begin
  SHGetSpecialFolderLocation(0, ASpecialFolderID, vSFolder);
  SHGetPathFromIDList(vSFolder, vSpecialPath);
  Result := vSpecialPath;
end;

function TSpecialFolders.GetCommonDocumentsFolder: string;
begin
  result := GetSpecialFolder(  CSIDL_COMMON_DOCUMENTS );
end;     

This works fine on my Windows 7 box and allows my app to write folders and files.

On a customers PC (I think its an older version of windows, because the screen shot has the word "start" on the start button) my app is unable to write files into that path.

On my PC the returned path is:

C:\ProgramData\

with me adding extra folders like so:

mycompany\myapp\logs\

mycompany\myapp\db\

with text files being saved in those folders.

On my customer's PC the function returns:

C:\Documents and Settings\All Users\Application Data

I am unable to write text files into the directory structure there.

Is there a better function I should be using, or a better common file folder?


Edit for SilverWarrior

these are the special folders on my customer's PC.

AppDataFolder : C:\Documents and Settings\Admin\Application Data CommonAppDataFolder : C:\Documents and Settings\All Users\Application Data CommonDocumentsFolder : C:\Documents and Settings\All Users\Documents LocalAppDataFolder : C:\Documents and Settings\Admin\Local Settings\Application Data MyDocumentsFolder : C:\Documents and Settings\Admin\My Documents

The LocalAppDataFolder has "Admin" in the path suggesting that admin rights would be needed. Is that right?

Michael Vincent
  • 1,620
  • 1
  • 20
  • 46
  • Tey run your Delphi as Administrator and try again. If that works from with in your Delphi then thesolution isto give the program admin rights I'll tell you that later – Jens Borrisholt Sep 24 '14 at 16:08
  • Expect write access to shared folders to be restricted – David Heffernan Sep 24 '14 at 16:55
  • No, @Jens, running with admin rights is never the solution. It's the hack that lets you *avoid* finding the solution. The solution in this case would appear to be to ask the customer's sysadmin to configure the environment properly. Another solution is to make the log and database directories configurable. But even then, the *configuration* still needs to be written somewhere. – Rob Kennedy Sep 24 '14 at 16:57

3 Answers3

3

The CSIDL_COMMON_APPDATA folder is protected by default and only adminstrators have write access to it.

To gain access for all users the administrator has to create a directory inside and grant the needed rights to the users. This task is typically done by an installer for your application (fi InnoSetup has also an option to grant the needed rights to such folders).

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
2

The problem is that you are trying to save that data into AppData folder which is intended for all users. By default writing into this folder requires Administrative rights.

So instead of using CSIDL_COMMON_APPDATA (AppData folder for all users) use CSIDL_LOCAL_APPDATA (curent users AppData folder). Writing to curent user AppData folder does not require elevated rights so it shoud work fine.

SilverWarior
  • 7,372
  • 2
  • 16
  • 22
0

Well, armed with info from here and a double-hop remote desktop connection to my customer's pc I went in prepared to do battle with the Windows permissions system - only to find that the problem was due to the application's ini file was set to read only.

Clickity click. Problem solved.

All comments about the common app data folder being restricted may still apply as the customer is running his account as administrator on XP SP3.

Thank you for your help.

Michael Vincent
  • 1,620
  • 1
  • 20
  • 46