Delphi ini file vista / xp / win7

Update: I added the following code:

function TSettingsForm.AppDataPath: string; //CSIDL_APPDATA Individual user Data //CSIDL_COMMON_APPDATA Common to Computer Data // works so long as people have at least IE 4. (and Win95 or better) var r: Bool; path: array[0..Max_Path] of Char; begin r := ShGetSpecialFolderPath(0, path, CSIDL_APPDATA, False) ; if r then result := path else result := ''; end; 

And I changed the setinifilename function (see below). It will not create a folder structure.

- End of update -

I'm for a while, which is what not to do. This is how I save my software settings. I just tested it on Vista, not registered as an administrator, and it gives me an error message, I can not write the ini file. So I assume that I should write data to the data folder? I have never used vista / win7 before and want this software to be compatible with Windows 2K +. What to do to save the settings. I also really did not want to mess with the registry, because every little bit that you add to it slows down the computer much more ... (or it seems)

Thanks for any input.

 procedure TSettingsForm.setinifilename; var filename:string; Path:string; begin filename:='key.ini'; path:=AppDataPath+'\MyCompanyName\ProductName\'; if NOT DirectoryExists(path) then CreateDir(path); inifilename:= path+filename; end; procedure TSettingsForm.SaveSettings; var appINI: TIniFile; begin appINI := TIniFile.Create(inifilename) ; try low:= Trunc (edt_low.value); high:=Trunc (edt_high.value); appINI.WriteInteger('SPEED','LOW',low); appINI.WriteInteger('SPEED','HIGH',high); appINI.WriteString('PROXY','SERVER',edtProxyServer.Text); appINI.WriteString('PROXY','PORT',edtProxyPort.Text); appINI.WriteString('PROXY','USERNAME',edtProxyUserName.Text); appINI.WriteString('PROXY','PASSWORD',edtProxyPass.Text); // status.text:='Saved Data'; finally appIni.Free; end; end; procedure TSettingsForm.GetSettings; Var appINI : TIniFile; begin appINI := TIniFile.Create(inifilename) ; try //if no last user return an empty string edt_low.value:= appINI.ReadInteger('SPEED','LOW',0); edt_high.value:= appINI.ReadInteger('SPEED','HIGH',0); low:= Trunc (edt_low.Value); high := Trunc (edt_high.Value); edtProxyServer.Text:=appINI.ReadString('PROXY','SERVER',''); edtProxyPort.Text:=appINI.ReadString('PROXY','PORT','0'); edtProxyUserName.Text:=appINI.ReadString('PROXY','USERNAME',''); edtProxyPass.Text:= appINI.ReadString('PROXY','PASSWORD',''); finally appINI.Free; end; end; 
+4
source share
3 answers

In Vista, your program is NOT allowed to write to the program file directory where your program is located.

Now you need to save your ini files in the AppData directory.

Description of how to do this in delphi: http://www.theabsolute.net/sware/delphivista.html#datafolder

And to be compatible with Vista / Windows 7, the rest of this web page will be a good guide.


For your update, you cannot immediately create CreateDir of more than one level. Use ForceDirectories instead :

  path:=AppDataPath+'\MyCompanyName\ProductName\'; if NOT DirectoryExists(path) then ForceDirectories(path); 

ps Do not be afraid to write the program settings in the registry. What is the registry for? In fact, it correctly handles settings for different users for you when you log in to different users. The registry works similarly in 98 / Vista / 7. While ini files have actually depreciated and are no longer used by Windows.

You say you don’t want to contact the registry, because "every little bit that you add to it slows down your computer much more." This is actually NOT true. A registry is just a database. And if it is 10 MB or 100 MB, the time difference that is required for access is not noticeable.

All of these companies are selling Registry Cleaner programs that are trying to preserve this tale. Using their cleaning products can do you more harm than good. All they need to do is destroy one or two important records, and you can be in deep doo-doo. Please read this article on Registry Cleaners, and especially the “Margin Performance Benefit” section, which explains that problems with Windows 98 and earlier with the Registry have been mostly fixed.

If your program adds more than 2 or 3 KB to the registry, this will be a lot, and this is a negligible amount. Use the registry. Right.

+8
source

You must use the ApplicationData directory for your application data. In Delphi, you can find this folder programmatically using the SHGetSpecialFolderLocation api function

Embarcadero has a FAQ page here.

+4
source

As already mentioned - do not save anything in the application folder.

You should split your configuration settings into two parts:

One part containing the settings that should work for the user is that part should be stored in COMMON_APPDATA.

The second part, containing individual user preferences (custom font selection, etc.) - this part should be stored in APPDATA

As for CreateDir, it’s true that you cannot create more than one level at a time, however in Delphi there is a ForceDirectories function that can do just that.

eg. ForceDirectories ('C: \ MyFolder \ subfolder \ SubSubFolder');

+2
source

All Articles