Help with preference files

Just starting out? Need help? Post your questions and find answers here.
georgej
User
User
Posts: 11
Joined: Wed Aug 17, 2005 5:21 pm

Help with preference files

Post by georgej »

I would be grateful if someone could help me please with a problem that I am having with program installation and then reading and
writing to a Preference file.

I have done searches on the forum and on Google and tried various different options and settings, both within my program code and in the
Inno Setup Compiler that I am using to create a setup file, without success. It is the first time I have used a preference file to
save some settings from within my code.

I have read this forum entry...
http://www.purebasic.fr/english/viewtop ... ry#p338746

The program itself is a simple utility program, with 5 saved variable settings in a preference file. When I create the exe and put this in a directory with a default preference file, it all works correctly. However, when I then went on to build a setup file where the exe file and preference file are placed together in the same installed program directory, when the program is started it works fine, apparently reading the default preferences file (there is error checking within the program code for the preference file). However, When I close the program and at this point the variables are saved, another preference file is created in the directory C:\Users\OEM\AppData\Local\VirtualStore\Program Files(x86)\. The program then apparently uses this preference file for subsequent reads and writes.

I have tried various changes of code within the program such as,

Code: Select all

 pathtofile.s =  GetPathPart(ProgramFilename()) + "Program.prefs"
for the reading and

Code: Select all

SetCurrentDirectory(GetPathPart(ProgramFilename()))
for the writing, without success. I have also changed various file and folder constants in the setup compiler without any success. Ideally, I am wanting to put a default preference file somewhere it can be accessed by the program and then uninstalled when the user removes the program from their computer.

I am using Windows 7 and the other operating systems the program is intended for are XP and Vista. I have noted a comment made about not using the SetCurrentDirectory() for this purpose and that the preference file should not be in the same directory as the exe file. Any advice or guidance as to where I would look to understand further about reading and writing to preference files and storing them correctly in different versions of Windows would be appreciated.

Thank you, George.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Help with preference files

Post by rsts »

Windows since Vista, discourages writing any data to the program directory and instead uses the app data/roaming location by default.

What privileges are you running with?

cheers
georgej
User
User
Posts: 11
Joined: Wed Aug 17, 2005 5:21 pm

Re: Help with preference files

Post by georgej »

Thank you rsts, for the quick reply.

I have administrator privileges. In the Inno Setup Compiler script I have also added the line - PrivilegesRequired=admin, as part of the installation.

I have picked up on the fact that what I have done is not the correct method. The main difficulty for me appears to be accessing the folder C:\Users\OEM\AppData\Local\VirtualStore\Program Files(x86)\. I have tried changing various constants in the setup compiler for the location of the preference file, but none appear to put a default preference file in this particular folder and then subsequently remove it again at uninstall.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Help with preference files

Post by rsts »

Is OEM your user name? Or the default that is getting set.

It is accessable. I do it regularly. Let me dig thru a little code.

In the meantime - see http://download.microsoft.com/download/ ... spaces.pdf

cheers
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Help with preference files

Post by ts-soft »

you have to use a path like this:

Code: Select all

MyPrefPath.s = GetEnvironmentVariable("APPDATA") + "\georgej\MyProgram.prefs"
You can also set this path in innosetup and so on.

greetings
Thomas
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Help with preference files

Post by rsts »

georgej
User
User
Posts: 11
Joined: Wed Aug 17, 2005 5:21 pm

Re: Help with preference files

Post by georgej »

Thank you both Thomas and rsts very much for your helpful replies. I now have a working solution.

Thomas, your code of

Code: Select all

MyPrefPath.s = GetEnvironmentVariable("APPDATA") + "\georgej\MyProgram.prefs"
worked fine for the reading of the preference file, but I found I had to change it slightly to

Code: Select all

MyPrefPath.s = GetEnvironmentVariable("APPDATA") + "\georgej\"
and then use

Code: Select all

CreatePreferences(MyPrefPath + "MyProgram.prefs")
for it to write to the preference file correctly.

In the Inno Setup Compiler I used the destination directory constant of DestDir: "{userappdata}\georgej" for the Preference file.

The Preference file is now placed in C:\Users\OEM\AppData\Roaming\georgej\ by the Setup program and I can read and write to it. The preference file and folder are deleted during the uninstall.

I am now going to change all the names to something more meaningful and I should be finished. I have spent quite a bit of time on this problem and have learnt a lot in the process. I hope this may assist others as well.

Thank you again. George
User avatar
Michael Vogel
Addict
Addict
Posts: 2811
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Help with preference files

Post by Michael Vogel »

Not sure if all postings try to give answers to the initial question...
...as I can't, just telling about some observations from the internet and real life :wink:

I read some fine things about Adam which is more than just an ereader.
The last days, some of my students were starting working with Galaxy Tabs and they seem to be satisfied (for now).
Post Reply