//unit IniHelper;
uses
Windows, SysUtils, Classes, SHFolder, IniFiles;
...
function GetSpecialFolderPath( Folder: Integer ):
string;
//const SHGFP_TYPE_CURRENT = 0;
var
Path:
Array [0..MAX_PATH]
of WideChar;
begin
if Windows.Succeeded( SHFolder.SHGetFolderPathW( 0, Folder, 0, SHGFP_TYPE_CURRENT, @Path[ 0 ] ) )
then
Result := Path
else
Result := '
';
end;
function ReadIniStream(
const Filename, Section, Ident:
String;
const FolderOverride:
String = '
' ): TStream;
var
FIniFile: TIniFile;
Check: Integer;
FName:
String;
MS: TStream;
begin
FName := GetSpecialFolderPath( CSIDL_LOCAL_APPDATA );
if FName = '
'
then
Exit;
if FolderOverride = '
'
then
FName := FName + '
\' + SysUtils.ChangeFileExt( SysUtils.ExtractFilename( ParamStr( 0 ) ), '
.' ) + '
\' + Filename
else
FName := FName + '
\' + FolderOverride + '
\' + Filename;
if not FileExists( FName )
then
Exit;
FIniFile := TIniFile.Create( FName );
try
MS := TMemoryStream.Create();
try
FIniFile.ReadBinaryStream( Section, Ident, MS );
MS.Position := 0;
Result.CopyFrom( MS, MS.Size );
Result.Position := 0;
finally
MS.Free;
end;
finally
FIniFile.Free;
end;
end;
function WriteIniStream(
const Filename, Section, Ident:
String;
const Value: TStream;
const FolderOverride:
String = '
' ): Boolean;
var
FIniFile: TIniFile;
FName:
String;
begin
Result := False;
FName := GetSpecialFolderPath( CSIDL_LOCAL_APPDATA );
if FName = '
'
then
Exit;
if FolderOverride = '
'
then
FName := FName + '
\' + SysUtils.ChangeFileExt( SysUtils.ExtractFilename( ParamStr( 0 ) ), '
.' )
else
FName := FName + '
\' + FolderOverride;
if not SysUtils.ForceDirectories( FName )
then
Exit;
if Value.Size = 0
then
Exit;
FIniFile := TIniFile.Create( FName + '
\' + Filename );
try
Value.Position := 0;
FIniFile.WriteBinaryStream( Section, Ident, Value );
finally
FIniFile.Free;
Result := True;
end;
end;