XMLPropStorage, use GetAppConfigDir? (SOLVED) lazarus / 31-LCL ---------------------------------------------------------------------- mijen67 · 2017-12-30 16:52 Is it possible to force XMLPropStorage object to use GetAppConfigDir(false)+'config.xml' as FileName parameter? I've tried typing in GetAppConfigDir(false)+'config.xml' (in the Object Inspector) but result (on windows 7) is not what I wanted. Kind regards, Michael ---------------------------------------------------------------------- Cyrax · 2017-12-30 18:55 Are you trying to input that string in the Object Inspector during a design time? It wont work, you need set it during your application run time. ---------------------------------------------------------------------- mijen67 · 2017-12-30 19:17 Quote from: Cyrax on December 30, 2017, 07:55:02 pmAre you trying to input that string in the Object Inspector during a design time? It wont work, you need set it during your application run time. I understand it must be run time, because path is different for each user, OS versions and for different platforms. However, can it be done, run time? Basically I want to follow the simplest approach from http://wiki.lazarus.freepascal.org/Remember_form_position_and_size and save Form1 properties Left, Top, Width, Height by using the TXMLPropStorage component. Unfortunately by default it saves the .xml files in the application folder and that is not the MS way (is it allowed on Mac's? Linux? ). Is it good practice? I want to keep things simple, and maybe I should just accept that it is the Lazarus way? And let the installer install the application in another folder than C:\Program Files\ ?  Or what would you recommend? ---------------------------------------------------------------------- molly · 2017-12-30 20:03 Quote from: mijen67 on December 30, 2017, 05:52:01 pmbut result (on windows 7) is not what I wanted. Quote from: mijen67 on December 30, 2017, 08:17:51 pmAnd let the installer install the application in another folder than C:\Program Files\ ?  Or what would you recommend? Perhaps you would want to read up with this article. Although a different installation path is allowed, it is not by default that when a user choses to install your application to another location that your program is allowed to read from and/or write to that same directory location. Your application has no way of knowing how a system of a user is configured so the only advise is to use the default directories that the OS intended for your application to be used. You might not like it, but if you go against that then your application will simply not run. The days of fat32 and users that provide themselves admin rights is far gone. Any application that tries to convince me that it requires admin rights to be able to install itself goes to directly to the trashcan (there are some exceptions to that rule but such software does other things then just being some end-user application). Even Lazarus installer can "go fish" for me. That is why GetAppConfigDir was implemented: to obey the OS rules as much as possible (and when possible). The result of GetAppConfigDir can somewhat be altered as written in its documentation. ---------------------------------------------------------------------- jamie · 2017-12-30 20:10 The GetAppConfigDir works because I use it and Windows returns the USER folder for data.  Place break points on the line and examine the content of the output..  If the FileName in the XML is showing the correct path, then maybe there is something wrong ? ---------------------------------------------------------------------- mijen67 · 2017-12-30 20:16 Quote from: molly on December 30, 2017, 09:03:30 pmAny application that tries to convince me that it requires admin rights to be able to install itself goes to directly to the trashcan (there are some exceptions to that rule but such software does other things then just being some end-user application). Even Lazarus installer can "go fish" for me. I agree. So your advice is? Not to use TXMLPropStorage? Or can I change it's FileName parameter run-time, so that I can get to use %appdata% instead? ---------------------------------------------------------------------- jamie · 2017-12-30 20:21 My point is, that function does work, I use it and it returns a string of the USER folder.. you need to conduct some test to verify the contents of this string to ensure it is in fact returning you a valid string at runtime ..  I use the Caption property as a test bug display  Caption := GetAppConfigDir(False)+yourFilename  if that passes, then assign it to the filename of the component.. then read it back again, displaying it to the forms Caption to make sure it remained that way..  if the file still insist on saving to the EXE folder then there is an issue with that component. I use TIniFile component for basic settings it works. ---------------------------------------------------------------------- molly · 2017-12-30 20:22 Quote from: mijen67 on December 30, 2017, 09:16:59 pmSo your advice is? Not to use TXMLPropStorage? You can use TXMLPropStorage, just require to change the default location. QuoteOr can I change it's FileName parameter run-time, so that I can get to use %appdata% instead? GetAppConfigDir should (on windows at least) return the correct path where your application is allowed to store data. You still havent' told us what GetAppConfigDir returned for you, so at this point in time i am unable to determine if there perhaps might be something amiss. AS user jamie wrote: it works for me tm ---------------------------------------------------------------------- mijen67 · 2017-12-30 20:25 Quote from: jamie on December 30, 2017, 09:10:25 pmThe GetAppConfigDir works because I use it and Windows returns the USER folder for data.  Place break points on the line and examine the content of the output..  If the FileName in the XML is showing the correct path, then maybe there is something wrong ? I'm not questioning GetAppConfigDir. It does work. I'm asking how to let XMLPropStorage object use it for finding path to .xml configuration file? I guess there is some timing involved. I cannot just set XMLPropStorage1.FileName := ...  anywhere in my project? ---------------------------------------------------------------------- molly · 2017-12-30 20:32 ah, in that case my apologies mijen67. I simply misunderstood your 'problem'. I'll try to dig up some of my old code, as i don't recall from memory if there is/was a particular order in which events should take place. It goes without saying that you can actually 'use' the component after the correct filename was set, as properties are read automatically from the config-file. So yes, order of things do matter. ---------------------------------------------------------------------- mijen67 · 2017-12-30 20:38 Quote from: molly on December 30, 2017, 09:32:05 pmah, in that case my apologies mijen67. I simply misunderstood your 'problem'. I'll try to dig up some of my old code, as i don't recall from memory if there is/was a particular order in which events should take place. It goes without saying that you can actually 'use' the component after the correct filename was set, as properties are read automatically from the config-file. So yes, order of things do matter. No problem, I definitely should have been more specific. Looking forward to seeing the proposal! ---------------------------------------------------------------------- molly · 2017-12-30 20:44 While my search is running, i realized something that could perhaps influence your results. GetAppConfigDir is able to return a path that doesn't already exist and that _might_ perhaps not be handled correctly by TXMLPropStorage . In that case the directory has to be created manually (forcedirectories comes to mind). ---------------------------------------------------------------------- molly · 2017-12-30 21:41 Funny thing is that in the example i'm quoting from i forced to use Application.exename and changing extension to xml  (i changed it to force it to use another path). It shows the basic usage. It is some silly buttons and labels that get stored, restored and changed (not important unless you wish to know more, just say so and i post complete project). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  ExtCtrls, XMLPropStorage; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    XButton1: TButton;    XButton2: TButton;    XButton3: TButton;    XButton4: TButton;    Button3: TButton;    XLabel1: TLabel;    XLabel2: TLabel;    XLabel3: TLabel;  XLabel4: TLabel;    Panel1: TPanel;    XMLPropStorage1: TXMLPropStorage;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure XMLPropStorage1RestoreProperties(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }  procedure TForm1.Button1Click(Sender: TObject);begin  // SaveSettings  XMLPropStorage1.Save;  Button1.Enabled := False;  Button2.Enabled := True;end; procedure TForm1.Button2Click(Sender: TObject);begin  Button2.Enabled := False;  Button3.Enabled := True;   // Simulate action  XButton1.Hide;  XButton2.Hide;  XButton3.Hide;  XButton4.Hide;   XButton1.Caption := 'This is button 1';   XLabel1.Caption := 'changed';  XLabel2.Color:= clRed;  XLabel1.Caption := 'changed';  XLabel2.Color:= clGreen;end; procedure TForm1.Button3Click(Sender: TObject);begin  // LoadSettings  XMLPropStorage1.Restore;  Button2.Enabled := True;end; procedure TForm1.FormCreate(Sender: TObject);begin  XMLPropStorage1.FileName := 'G:\Temp\SomeFileStorage.xml';  // initialize 'action' buttons  Button1.Enabled := True;  Button2.Enabled := False;  Button3.Enabled := False;end; end.  That worked for me and should work the way you want to, by changing: Code: [Select]  XMLPropStorage1.FileName := 'G:\Temp\SomeFileStorage.xml'; into Code: [Select]  XMLPropStorage1.FileName := GetAppConfigDir(False)+'SomeFileName'; As said in my previous post, make sure that the path that GetAppConfigDir returns actually exist. In case you still have issues then please describe them and also state lazarus and fpc version that you are using. ---------------------------------------------------------------------- mijen67 · 2018-01-01 14:49 Quote from: molly on December 30, 2017, 10:41:37 pmThat worked for me and should work the way you want to, by changing: Code: [Select]  XMLPropStorage1.FileName := 'G:\Temp\SomeFileStorage.xml'; into Code: [Select]  XMLPropStorage1.FileName := GetAppConfigDir(False)+'SomeFileName'; As said in my previous post, make sure that the path that GetAppConfigDir returns actually exist. In case you still have issues then please describe them and also state lazarus and fpc version that you are using. Perfect, thanks! I added an initialization section to the main unit with the following contents: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i fileConfigP ath:=GetAppConfigDir(False);IncludeTrailingPathDelimiter(fileConfigPat h);ForceDirectories(fileConfigPath); When a form has an XMLPropStorage component, I can just add: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i XMLPropStorage1.FileName:=fileConfigPath+Self.Name+'.xml'; to each .FormCreate method. Now it is working, and the program is saving data in a valid location. How do I mark the thread "solved"? ---------------------------------------------------------------------- molly · 2018-01-01 15:13 Quote from: mijen67 on January 01, 2018, 03:49:11 pmPerfect, thanks! You're welcome and glad that it worked for you Indeed you can also use your initialization section to setup some things like you did. Perhaps as a small tip: I personally prefer to use a TDataModule (that gets created before the first/main form) that has such features nicely tucked away as that allows these implementations to be easily re-used for other projects). But an additional (non visual) unit that has some global procedures/functions and/or initialization section works just as well. As long as it works for you then you should be good to go no matte what you use. QuoteHow do I mark the thread "solved"? Unfortunately (and in 2018) the forum software does not seem to have a standard solution integrated for marking a thread as solved. In case you are the starter of the thread, then you can edit your first post and insert [SOLVED] before the original topic title. Of course you would have to (re)post that message as well for the changes to take effect. Happy Coding ! ---------------------------------------------------------------------- mijen67 · 2018-01-02 10:55 Quote from: molly on January 01, 2018, 04:13:41 pmPerhaps as a small tip: I personally prefer to use a TDataModule (that gets created before the first/main form) that has such features nicely tucked away as that allows these implementations to be easily re-used for other projects). But an additional (non visual) unit that has some global procedures/functions and/or initialization section works just as well. Happy Coding ! Thanks! I think above hint is very important and would like to try out that idea. I've searched for TDataModule in the components list, Ctrl+Alt+P without luck. So I'm assuming it is a customly defined class instantiated into an object somewhere in the initialization and/or startup sequence. Or? I looked into the Wiki regarding startup and initialization and although somewhat informative, there are no examples indicating how this can be done, see http://wiki.freepascal.org/Startup and http://wiki.freepascal.org/Initialization.  In my applications .lpr file I have Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i begin  RequireDerivedFormResource:=True;  Application.Initialize;  // Insert instantiation of TDataModule here, or?  Application.CreateForm(TfrmMain, frmMain);  Application.CreateForm(TfrmClass, frmClass);  Application.CreateForm(TfrmGroups, frmGroups);  Application.CreateForm(TfrmStudent, frmStudent);  Application.Run;end.Is above location correct? I realize there are multiple ways to accomplish custom application initialization, and that there are several pros and cons for each method. And hence there probably is no "best practice"? But it would be great to see an example of how this can be done. Could you please elaborate on your hint? (should we move this to a new thread? feel free to instantiate a new thread)