Recent

Author Topic: How find path to 'application data' in XP, 2k3, vista, 7 ?  (Read 25692 times)

seba22

  • Full Member
  • ***
  • Posts: 136
Welcome,

I need to find patch for Application Data.

I have try that code:

Code: [Select]
GetMem(P,128);
GetTempPath(144, P);
S := PChar(P);
app_data_path := Copy(S, 1, Length(S)-14);
FreeMem(P);

On XP - c:\documents and settings\username 
On 7 - c:\users\name\AppData  - corect !

I didn't test it on any other OS :(


I need function what always return app data.

Any idea (including all languages).

Help, please :)

Regards


paweld

  • Hero Member
  • *****
  • Posts: 1561
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #1 on: May 13, 2010, 08:30:37 pm »
On 7 - c:\users\name\AppData  - corect !
not correct.
correct on win 7 - c:\users\name\appdata\roaming\

path for application data:
Code: [Select]
GetEnvironmentVariable('appdata')or local application data:
Code: [Select]
GetEnvironmentVariable('localappdata')
for ms OS: 2000prof/2000server/xp/2003/vista/2008/7

Best regards / Pozdrawiam
paweld
Best regards / Pozdrawiam
paweld

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #2 on: May 13, 2010, 09:19:33 pm »
There are a couple of related functions for this in SysUtils

GetAppConfigDir(false) is probably what you are looking for

seba22

  • Full Member
  • ***
  • Posts: 136
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #3 on: May 14, 2010, 08:02:47 am »
On 7 - c:\users\name\AppData  - corect !
not correct.
correct on win 7 - c:\users\name\appdata\roaming\

path for application data:
Code: [Select]
GetEnvironmentVariable('appdata')or local application data:
Code: [Select]
GetEnvironmentVariable('localappdata')
for ms OS: 2000prof/2000server/xp/2003/vista/2008/7

Best regards / Pozdrawiam
paweld

Hello,

I just try and don't work. I can't find what parameter is missing...


Code: [Select]
showmessage(GetEnvironmentVariable('localappdata'));
It say's

Code: [Select]
unit1.pas(424,40) Error: Wrong number of parameters specified for call to "GetEnvironmentVariable"

Regards
PS: Witam rodaka ;)

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #4 on: May 14, 2010, 08:57:12 am »
Code: [Select]
showmessage(GetEnvironmentVariable('localappdata'));
It say's

Code: [Select]
unit1.pas(424,40) Error: Wrong number of parameters specified for call to "GetEnvironmentVariable"

Compiles and runs here. Parameters 'appdata' and 'localappdata' both return empty strings on my Linux.
GetAppConfigDir(false) returns a useful path. (as noted by Laksen).

Are you using Lazarus? You could easily check the required parameters for GetEnvironmentVariable() by Ctrl-Space. You can also check where your version of GetEnvironmentVariable() is defined by Ctrl-MouseClick. It may not be the SysUtils version.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

seba22

  • Full Member
  • ***
  • Posts: 136
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #5 on: May 14, 2010, 10:16:03 am »
Welcome,

Yes, i'm using Lazarus.
Just now i make update to newest version from svn.


I use Your suggestion and hit ctrl + space.
I got 2 functions called GetEnvironmentVariable.
First require few parameters, second require only one (string).


When i click on GetEnvironmentVariable i got source:

Code: [Select]
function GetEnvironmentVariable(lpName:LPCSTR; lpBuffer:LPSTR; nSize:DWORD):DWORD; external 'kernel32' name 'GetEnvironmentVariableA';

I would like select, this first, what require only one string...

If someone have idea... please help...

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1933
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #6 on: May 14, 2010, 10:24:53 am »
This looks like the function defined in unit windows.
You need the one in sysutils:
http://www.freepascal.org/docs-html/rtl/sysutils/getenvironmentvariable.html

seba22

  • Full Member
  • ***
  • Posts: 136
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #7 on: May 14, 2010, 11:03:48 am »
Yes, i notice that.
But how can i select right option ?.


I have both Windows and SysUtils declared.

Maybe i could rename that (wrong) function...  ?

 
 

faber

  • Guest
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #8 on: May 14, 2010, 11:07:12 am »
sysutils.GetEnvironmentVariable();

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4660
  • I like bugs.
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #9 on: May 14, 2010, 11:13:45 am »
Yes, i notice that.
But how can i select right option ?.
I have both Windows and SysUtils declared.
Maybe i could rename that (wrong) function...  ?

SysUtils.GetEnvironmentVariable().
Or better yet, remove Windows unit dependency and your code will be portable.

BTW, my advise earlier was little wrong.
"Ctrl + Shift + Space" shows the function parameter signature.
"Ctrl + Space" shows the available variables that match the function parameter.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

thierrybo

  • Full Member
  • ***
  • Posts: 146
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #10 on: May 14, 2010, 04:03:46 pm »
Hi,

If you want to stay multiplaform compatible (and even id not !), use GetAppConfigFile and GetAppConfigDir from SysUtils, it is purposed built :

XP:

Code: [Select]
GetAppConfigDir(True) -> C:\D&S\All Users\Application Data\project1\
GetAppConfigDir(False) -> C:\D&S\user\Local Settings\Application Data\project1\
GetAppConfigFile(True) -> C:\D&S\All Users\Application Data\project1\project1.cfg
GetAppConfigFile(False) -> C:\D&S\user\Local Settings\Application Data\project1\project1.cfg

Seven:

Code: [Select]
[code]GetAppConfigDir(True) -> C:\ProgramData\project1\
GetAppConfigDir(False) -> C:\Users\user\AppData\local\project1\
GetAppConfigFile(True) -> C:\ProgramData\project1\project1.cfg
GetAppConfigFile(False) -> C:\Users\user\AppData\local\project1\project1.cfg
[/code]

Linux:

Code: [Select]
GetAppConfigDir(True) -> /etc/project1/
GetAppConfigDir(False) -> /home/user/.config/project1/
GetAppConfigFile(True) -> /etc/project1.cfg (buggy, for me should be /etc/project1/project1.cfg)
GetAppConfigFile(False) ->/home/user/.config/project1.cfg (buggy, for me should be /home/user/.config/project1/project1.cfg)

seba22

  • Full Member
  • ***
  • Posts: 136
Re: How find path to 'application data' in XP, 2k3, vista, 7 ?
« Reply #11 on: May 14, 2010, 04:15:02 pm »
sysutils.GetEnvironmentVariable();

Thank You!
It help ;)

 

TinyPortal © 2005-2018