[Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

[Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Peter Williams
Hi All, 

I use this procedure to save an inifile using the standard IniFiles unit. The problem I have is that when an .ini file already exists I cannot overwrite it. I need the TextFiles equivalent of Rewrite() for Inifiles.

This line:
  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
... when the ini file exists fails with an error message of 'unable to create appname.ini'. This is because it cannot create an ini file which already exists.

How then do I rewrite the .ini file?

Uses IniFiles;

procedure TMain_Form.SaveIniFile;
var
  ConfigIni: TIniFile;
  x, y: integer;
begin
  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  // e.g. fred.exe becomes fred.ini
  try
    for x := 1 to max_pats do
      begin
        if patterns_list[x].Count > 0 then
          begin
            ConfigIni.WriteString('Pattern' + inttostr(x), 'Name', Patterns_list[x].Strings[0]);
            for y := 1 to patterns_list[x].Count - 1 do
              ConfigIni.WriteString('Pattern' + inttostr(x), 'Line' + inttostr(y), Patterns_list[x].Strings[y]);
          end;
      end;

    ConfigIni.WriteInteger('General', 'Auto Generation Delay', Timer1.Interval);
    if current_color_scheme = krgy then
      ConfigIni.WriteString('General', 'Current Color Scheme', 'krgy')
    else
      ConfigIni.WriteString('General', 'Current Color Scheme', 'kmcw');
    ConfigIni.WriteBool('General', 'Sound Wanted', Sound_wanted);

    ConfigIni.UpdateFile;
  finally
    ConfigIni.Free;
  end;
end;
{---------------------------------------------------------}

Best Regards,
Peter / pew

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
ik
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

ik
2011/3/15 Peter Williams <[hidden email]>
Hi All, 

I use this procedure to save an inifile using the standard IniFiles unit. The problem I have is that when an .ini file already exists I cannot overwrite it. I need the TextFiles equivalent of Rewrite() for Inifiles.

This line:
  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
... when the ini file exists fails with an error message of 'unable to create appname.ini'. This is because it cannot create an ini file which already exists.

How then do I rewrite the .ini file?

Uses IniFiles;

procedure TMain_Form.SaveIniFile;
var
  ConfigIni: TIniFile;
  x, y: integer;
begin
  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  // e.g. fred.exe becomes fred.ini
  try
    for x := 1 to max_pats do
      begin
        if patterns_list[x].Count > 0 then
          begin
            ConfigIni.WriteString('Pattern' + inttostr(x), 'Name', Patterns_list[x].Strings[0]);
            for y := 1 to patterns_list[x].Count - 1 do
              ConfigIni.WriteString('Pattern' + inttostr(x), 'Line' + inttostr(y), Patterns_list[x].Strings[y]);
          end;
      end;

    ConfigIni.WriteInteger('General', 'Auto Generation Delay', Timer1.Interval);
    if current_color_scheme = krgy then
      ConfigIni.WriteString('General', 'Current Color Scheme', 'krgy')
    else
      ConfigIni.WriteString('General', 'Current Color Scheme', 'kmcw');
    ConfigIni.WriteBool('General', 'Sound Wanted', Sound_wanted);

    ConfigIni.UpdateFile;
  finally
    ConfigIni.Free;
  end;
end;
{---------------------------------------------------------}

Best Regards,
Peter / pew

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Do you have permission for writing the content of the file ?

Ido

LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il



--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

zeljko
In reply to this post by Peter Williams
On Tuesday 15 of March 2011 05:25:41 Peter Williams wrote:
> Hi All,
> I use this procedure to save an inifile using the standard IniFiles unit.
> The problem I have is that when an .ini file already exists I cannot
> overwrite it. I need the TextFiles equivalent of Rewrite() for Inifiles.
> This line:  ConfigIni :=
> TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));... when the
> ini file exists fails with an error message of 'unable to create
> appname.ini'. This is because it cannot create an ini file which already
> exists. How then do I rewrite the .ini file?

I've seen such problem with fpc-2.4.3 under win7 64bit only.
My workaround is to use TMemIniFile instead of TIniFile. So try that first.

zeljko

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Lukasz Sokol
In reply to this post by Peter Williams
On 15/03/2011 04:25, Peter Williams wrote:

>
> Hi All,
> I use this procedure to save an inifile using the standard IniFiles unit. The problem I have is that when an .ini file already exists I cannot overwrite it. I need the TextFiles equivalent of Rewrite() for Inifiles.
> This line:  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));... when the ini file exists fails with an error message of 'unable to create appname.ini'. This is because it cannot create an ini file which already exists.
> How then do I rewrite the .ini file?
> Uses IniFiles;
> procedure TMain_Form.SaveIniFile;var  ConfigIni: TIniFile;  x, y: integer;begin  ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));  // e.g. fred.exe becomes fred.ini  try    for x := 1 to max_pats do      begin        if patterns_list[x].Count > 0 then          begin            ConfigIni.WriteString('Pattern' + inttostr(x), 'Name', Patterns_list[x].Strings[0]);            for y := 1 to patterns_list[x].Count - 1 do              ConfigIni.WriteString('Pattern' + inttostr(x), 'Line' + inttostr(y), Patterns_list[x].Strings[y]);          end;      end;
>     ConfigIni.WriteInteger('General', 'Auto Generation Delay', Timer1.Interval);    if current_color_scheme = krgy then      ConfigIni.WriteString('General', 'Current Color Scheme', 'krgy')    else      ConfigIni.WriteString('General', 'Current Color Scheme', 'kmcw');    ConfigIni.WriteBool('General', 'Sound Wanted', Sound_wanted);
>     ConfigIni.UpdateFile;  finally    ConfigIni.Free;  end;end;{---------------------------------------------------------}
> Best Regards,Peter / pew    
>
>

{$ define rewrite } ?

If by 'rewrite' you mean 'to clear it and then write it again', then I'd just plain DeleteFile, then create
it again.
if by 'rewrite' you mean 'to load its contents and_then clear them and_then write new content' then
I'd use the TMemIniFile that can load contents from TString[s][list].

L.


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Bart
In reply to this post by Peter Williams
On 3/15/11, Peter Williams <[hidden email]> wrote:

> ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));

You really should not do that in most OS's, even on Windows it is
deprecated since win9x.
Most times you will not have write permissions there.
Also under *nix this location might not be what you expect it to be...

Use GetAppConfigDir to determine where to put INI files.

Bart

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Sven Barth
Am 15.03.2011 12:45, schrieb Bart:

> On 3/15/11, Peter Williams<[hidden email]>  wrote:
>
>> ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
>
> You really should not do that in most OS's, even on Windows it is
> deprecated since win9x.
> Most times you will not have write permissions there.
> Also under *nix this location might not be what you expect it to be...
>
> Use GetAppConfigDir to determine where to put INI files.

It took me a while until I understand what you wanted to tell... maybe
you should have mentioned directly that you mean "writing in the
application's directory".

While I agree with you that modifyable configuration should be put into
the proper configuration locations I personally see no problem with
having a readonly configuration file in the application's directory
(yes, I'm aware that this is not cross platform). So the problem could
be that the INI file is always opened as "read/write", while at the
beginning "read" should be sufficient. This could explain why
TMemIniFile works for such cases.

Regards,
Sven

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Hans-Peter Diettrich
Sven Barth schrieb:

> It took me a while until I understand what you wanted to tell... maybe
> you should have mentioned directly that you mean "writing in the
> application's directory".
>
> While I agree with you that modifyable configuration should be put into
> the proper configuration locations I personally see no problem with
> having a readonly configuration file in the application's directory
> (yes, I'm aware that this is not cross platform).

Also consider multi-user environments, or company (network)
installations managed by an admin.

DoDi


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Peter Williams
In reply to this post by Bart
Hi Bart,

> Date: Tue, 15 Mar 2011 12:45:49 +0100
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).
>
> On 3/15/11, Peter Williams <[hidden email]> wrote:
>
> > ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
>
> You really should not do that in most OS's, even on Windows it is
> deprecated since win9x.
> Most times you will not have write permissions there.
> Also under *nix this location might not be what you expect it to be...
>
> Use GetAppConfigDir to determine where to put INI files.

I found the declaration for function GetAppConfigDir in osutilsh.inc but I cannot find the actual function (I am looking in the wrong file).

Function GetAppConfigDir(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;

What is the "Global : Boolean" parameter that it takes? Can someone please give me an example as to how it is called?

Thanks,
PEW

osutilsh.inc ---

{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2000 by Michael Van Canneyt
    member of the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

Type TExecuteFlags = Set of ( ExecInheritsHandles);

{ OS handling utilities }

{$ifdef HAS_OSERROR}
Function GetLastOSError : Integer;
{$endif}
Procedure RaiseLastOSError;
Function GetEnvironmentVariable(Const EnvVar : String) : String;
Function GetEnvironmentVariableCount : Integer;
Function GetEnvironmentString(Index : Integer) : String;
{$IFDEF HAS_SLEEP}
procedure Sleep(milliseconds: Cardinal);
{$ENDIF}
function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString;Flags:TExecuteFlags=[]):integer;

Function GetTempDir(Global : Boolean) : String;
Function GetTempDir : String;
Function GetTempFileName(Const Dir,Prefix : String) : String;
Function GetTempFileName : String;
{ For windows compatibility }
function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
Function GetAppConfigDir(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
Function GetUserDir : String;
Function VendorName : String;
Function ApplicationName : String;

Const
  ConfigExtension : String = '.cfg';
  SysConfigDir    : String = '';

Type
  TGetVendorNameEvent  = Function : String;
  TGetAppNameEvent  = Function : String;
  TGetTempDirEvent  = Function (Global : Boolean) : String;
  TGetTempFileEvent = Function (Const Dir,Prefix : String) : String;

Type
  TEventType = (etCustom,etInfo,etWarning,etError,etDebug);

Var
  OnGetVendorName      : TGetVendorNameEvent;
  OnGetApplicationName : TGetAppNameEvent;
  OnGetTempDir         : TGetTempDirEvent;
  OnGetTempFile        : TGetTempFileEvent;

>
> Bart
>
> --
> _______________________________________________
> Lazarus mailing list
> [hidden email]
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Sven Barth
Am 15.03.2011 15:24, schrieb Peter Williams:

> Hi Bart,
>
>  > Date: Tue, 15 Mar 2011 12:45:49 +0100
>  > From: [hidden email]
>  > To: [hidden email]
>  > Subject: Re: [Lazarus] Unable to rewrite .ini file which exists...
> error unable to create .ini file. (need to rewrite it).
>  >
>  > On 3/15/11, Peter Williams <[hidden email]> wrote:
>  >
>  > > ConfigIni :=
> TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
>  >
>  > You really should not do that in most OS's, even on Windows it is
>  > deprecated since win9x.
>  > Most times you will not have write permissions there.
>  > Also under *nix this location might not be what you expect it to be...
>  >
>  > Use GetAppConfigDir to determine where to put INI files.
>
> I found the declaration for function GetAppConfigDir in osutilsh.inc but
> I cannot find the actual function (I am looking in the wrong file).
>

If you search the function by clicking on the identifier in Lazarus
while pressing "Ctrl" you should be able to use "Ctrl + Shift + Down" to
find the declaration.

Alternatively you can search the directory "%lazarus%\fpc\%version%\rtl"
to find the implementation (might be some file in the win or win32
directory).

> Function GetAppConfigDir(Global : Boolean) : String;
> Function GetAppConfigFile(Global : Boolean) : String;
> Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
>
> What is the "Global : Boolean" parameter that it takes? Can someone
> please give me an example as to how it is called?

If global is true the system's configuration directory is returned, if
false the current user's directory.

E.g.:

Windows 7:
GetAppConfigDir(True):
C:\ProgramData\AppName
GetAppContigDir(False):
C:\Users\Sven\AppData\Local\AppName (or Roaming, I haven't tested, to be
honest)

Linux:
GetAppConfigDir(True):
/etc/AppName
GetAppConfigDir(False):
/home/sven/.config/AppName

When you use GetAppConfigFile instead, AppName.cfg is returned instead
of AppName.

See also:
http://www.freepascal.org/docs-html/rtl/sysutils/getappconfigdir.html
http://www.freepascal.org/docs-html/rtl/sysutils/getappconfigfile.html

Regards,
Sven

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Sven Barth
In reply to this post by Peter Williams
Am 15.03.2011 15:24, schrieb Peter Williams:
> Can someone
> please give me an example as to how it is called?

Before I forget:

The functions only return a filename, they don't create the file. So you
must do that yourself (you can simply pass the result to TIniFile.Create
for example).

Regards,
Sven

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Peter Williams
In reply to this post by Peter Williams
Hi,


From: [hidden email]
To: [hidden email]
Date: Wed, 16 Mar 2011 01:24:46 +1100
Subject: Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Hi Bart,

> Date: Tue, 15 Mar 2011 12:45:49 +0100
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).
>
> On 3/15/11, Peter Williams <[hidden email]> wrote:
>
> > ConfigIni := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
>
> You really should not do that in most OS's, even on Windows it is
> deprecated since win9x.
> Most times you will not have write permissions there.
> Also under *nix this location might not be what you expect it to be...
>
> Use GetAppConfigDir to determine where to put INI files.

I found the declaration for function GetAppConfigDir in osutilsh.inc but I cannot find the actual function (I am looking in the wrong file).

Function GetAppConfigDir(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;

What is the "Global : Boolean" parameter that it takes? Can someone please give me an example as to how it is called?



I just tried calling Str1 := GetAppConfigDir(true); ShowMessage(Str1);

and my app is called Color_Life.exe and it returns "C:\ProgramData\Color_Life\". Since the folder "C:\ProgramData\" exists and it's subfolder "\Color_Life\" does NOT exist, then does this mean that I need to create it myself, or will my call to one of the "MemIniFiles" procedures create the folder for me?

Best Regards,
PEW

Thanks,
PEW

osutilsh.inc ---

{
    This file is part of the Free Pascal run time library.
    Copyright (c) 1999-2000 by Michael Van Canneyt
    member of the Free Pascal development team

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}

Type TExecuteFlags = Set of ( ExecInheritsHandles);

{ OS handling utilities }

{$ifdef HAS_OSERROR}
Function GetLastOSError : Integer;
{$endif}
Procedure RaiseLastOSError;
Function GetEnvironmentVariable(Const EnvVar : String) : String;
Function GetEnvironmentVariableCount : Integer;
Function GetEnvironmentString(Index : Integer) : String;
{$IFDEF HAS_SLEEP}
procedure Sleep(milliseconds: Cardinal);
{$ENDIF}
function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
function ExecuteProcess(Const Path: AnsiString; Const ComLine: Array of AnsiString;Flags:TExecuteFlags=[]):integer;

Function GetTempDir(Global : Boolean) : String;
Function GetTempDir : String;
Function GetTempFileName(Const Dir,Prefix : String) : String;
Function GetTempFileName : String;
{ For windows compatibility }
function GetTempFileName(Dir,Prefix: PChar; uUnique: DWORD; TempFileName: PChar):DWORD;
Function GetAppConfigDir(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean) : String;
Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
Function GetUserDir : String;
Function VendorName : String;
Function ApplicationName : String;

Const
  ConfigExtension : String = '.cfg';
  SysConfigDir    : String = '';

Type
  TGetVendorNameEvent  = Function : String;
  TGetAppNameEvent  = Function : String;
  TGetTempDirEvent  = Function (Global : Boolean) : String;
  TGetTempFileEvent = Function (Const Dir,Prefix : String) : String;

Type
  TEventType = (etCustom,etInfo,etWarning,etError,etDebug);

Var
  OnGetVendorName      : TGetVendorNameEvent;
  OnGetApplicationName : TGetAppNameEvent;
  OnGetTempDir         : TGetTempDirEvent;
  OnGetTempFile        : TGetTempFileEvent;

>
> Bart
>
> --
> _______________________________________________
> Lazarus mailing list
> [hidden email]
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-- _______________________________________________ Lazarus mailing list [hidden email] http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

Sven Barth
Am 15.03.2011 15:51, schrieb Peter Williams:
> I just tried calling Str1 := GetAppConfigDir(true); ShowMessage(Str1);
>
> and my app is called Color_Life.exe and it returns
> "C:\ProgramData\Color_Life\". Since the folder "C:\ProgramData\" exists
> and it's subfolder "\Color_Life\" does NOT exist, then does this mean
> that I need to create it myself, or will my call to one of the
> "MemIniFiles" procedures create the folder for me?

You need to create the path yourself. As I said: those functions just
return a string. What you do with it, is up to you.

Regards,
Sven

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

[Lazarus] How to create a directory from a path string? (cross-platform needed).

Peter Williams
Hi Sven,

> Date: Tue, 15 Mar 2011 16:02:55 +0100
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).
>
> Am 15.03.2011 15:51, schrieb Peter Williams:
> > I just tried calling Str1 := GetAppConfigDir(true); ShowMessage(Str1);
> >
> > and my app is called Color_Life.exe and it returns
> > "C:\ProgramData\Color_Life\". Since the folder "C:\ProgramData\" exists
> > and it's subfolder "\Color_Life\" does NOT exist, then does this mean
> > that I need to create it myself, or will my call to one of the
> > "MemIniFiles" procedures create the folder for me?
>
> You need to create the path yourself. As I said: those functions just
> return a string. What you do with it, is up to you.
var
  str1 : string;
begin
  str1 := GetAppConfigDir( true );
  ShowMessage( Str1 );
  // note: for Fred.exe returns C:\ProgramData\Fred\
  if not DirectoryExists(Str1) then
    // Create Directory Str1
    // call to 'mkdir' ??? what about on Linux??? and other platforms?

What is the cross-platform way for me to create the directory in Str1 (as returned by GetAppConfigDir) ?

I would assume that I need to ShellExecute a command for 'mkdir' for Windows, correct? How about for Linux and Macs, etc?

I found this page:


But I have to admit that I was totally lost in what it was trying to explain. 

An example, please?

Best Regards,
PEW

>
> Regards,
> Sven
>
> --
> _______________________________________________
> Lazarus mailing list
> [hidden email]
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create .ini file. (need to rewrite it).

leledumbo
Administrator
In reply to this post by Peter Williams
Weird, I never had such a problem. In fact, one of my application uses it (the others use it implicitly via TIniPropStorage). The only problem I can think of is somehow the file is opened as well by another code and isn't closed. What if you try to debug using gdb so that you can see exactly where it fails.
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How to create a directory from a path string? (cross-platform needed).

Henry Vermaak
In reply to this post by Peter Williams
2011/3/15 Peter Williams <[hidden email]>:

> Hi Sven,
>
>> Date: Tue, 15 Mar 2011 16:02:55 +0100
>> From: [hidden email]
>> To: [hidden email]
>> Subject: Re: [Lazarus] Unable to rewrite .ini file which exists... error
>> unable to create .ini file. (need to rewrite it).
>>
>> Am 15.03.2011 15:51, schrieb Peter Williams:
>> > I just tried calling Str1 := GetAppConfigDir(true); ShowMessage(Str1);
>> >
>> > and my app is called Color_Life.exe and it returns
>> > "C:\ProgramData\Color_Life\". Since the folder "C:\ProgramData\" exists
>> > and it's subfolder "\Color_Life\" does NOT exist, then does this mean
>> > that I need to create it myself, or will my call to one of the
>> > "MemIniFiles" procedures create the folder for me?
>>
>> You need to create the path yourself. As I said: those functions just
>> return a string. What you do with it, is up to you.
> var
>   str1 : string;
> begin
>   str1 := GetAppConfigDir( true );
>   ShowMessage( Str1 );
>   // note: for Fred.exe returns C:\ProgramData\Fred\
>   if not DirectoryExists(Str1) then
>     // Create Directory Str1
>     // call to 'mkdir' ??? what about on Linux??? and other platforms?
> What is the cross-platform way for me to create the directory in Str1 (as
> returned by GetAppConfigDir) ?
> I would assume that I need to ShellExecute a command for 'mkdir' for
> Windows, correct? How about for Linux and Macs, etc?
> I found this page:
> http://wiki.lazarus.freepascal.org/Executing_External_Programs
> But I have to admit that I was totally lost in what it was trying to
> explain.
> An example, please?

Use function ForceDirectories in sysutils.

Henry

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How to create a directory from a path string? (cross-platform needed).

silvioprog
In reply to this post by Peter Williams
2011/3/15 Peter Williams <[hidden email]>:
(...)
var
  Str1 : string;
begin
  Str1 := GetAppConfigDir(False);
  if not DirectoryExists(Str1) then
    ForceDirectory(Str1);
(...)

Good luck. ;)

--
Silvio Clécio
=================================================
Blog - silvioprog.com.br
Twitter - twitter.com/silvioprog
LazSolutions - code.google.com/p/lazsolutions
Lazarus-BR - http://groups.google.com.br/group/lazarus-br?hl=pt-BR
=================================================
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How to create a directory from a path string? (cross-platform needed).

William Oliveira Ferreira
Could Try this one?
http://www.freepascal.org/docs-html/rtl/sysutils/createdir.html
________________________________
William de Oliveira Ferreira
Bacharel em Sistemas de Informação



2011/3/15 silvioprog <[hidden email]>
2011/3/15 Peter Williams <[hidden email]>:
(...)
var
  Str1 : string;
begin
 Str1 := GetAppConfigDir(False);
  if not DirectoryExists(Str1) then
   ForceDirectory(Str1);
(...)

Good luck. ;)

--
Silvio Clécio
=================================================
Blog - silvioprog.com.br
Twitter - twitter.com/silvioprog
LazSolutions - code.google.com/p/lazsolutions
Lazarus-BR
- http://groups.google.com.br/group/lazarus-br?hl=pt-BR
=================================================
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How to create a directory from a path string? (cross-platform needed).

silvioprog
2011/3/15 William Oliveira Ferreira <[hidden email]>:
> Could Try this one?
> http://www.freepascal.org/docs-html/rtl/sysutils/createdir.html
> ________________________________
> William de Oliveira Ferreira
> Bacharel em Sistemas de Informação

CreateDir does not create recursive path.

> 2011/3/15 silvioprog <[hidden email]>
>>
>> 2011/3/15 Peter Williams <[hidden email]>:
>> (...)
>> var
>>   Str1 : string;
>> begin
>>  Str1 := GetAppConfigDir(False);
>>   if not DirectoryExists(Str1) then
>>    ForceDirectory(Str1);
>> (...)
>>
>> Good luck. ;)

--
Silvio Clécio
=================================================
Blog - silvioprog.com.br
Twitter - twitter.com/silvioprog
LazSolutions - code.google.com/p/lazsolutions
Lazarus-BR - http://groups.google.com.br/group/lazarus-br?hl=pt-BR
=================================================

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How to create a directory from a path string? (cross-platform needed).

Michael Van Canneyt
In reply to this post by Peter Williams


On Wed, 16 Mar 2011, Peter Williams wrote:

> Hi Sven,
>
> > Date: Tue, 15 Mar 2011 16:02:55 +0100
> > From: [hidden email]
> > To: [hidden email]
> > Subject: Re: [Lazarus] Unable to rewrite .ini file which exists... error unable to create
> .ini file. (need to rewrite it).
> >
> > Am 15.03.2011 15:51, schrieb Peter Williams:
> > > I just tried calling Str1 := GetAppConfigDir(true); ShowMessage(Str1);
> > >
> > > and my app is called Color_Life.exe and it returns
> > > "C:\ProgramData\Color_Life\". Since the folder "C:\ProgramData\" exists
> > > and it's subfolder "\Color_Life\" does NOT exist, then does this mean
> > > that I need to create it myself, or will my call to one of the
> > > "MemIniFiles" procedures create the folder for me?
> >
> > You need to create the path yourself. As I said: those functions just
> > return a string. What you do with it, is up to you.
> var
>   str1 : string;
> begin
>   str1 := GetAppConfigDir( true );
>   ShowMessage( Str1 );
>   // note: for Fred.exe returns C:\ProgramData\Fred\
>   if not DirectoryExists(Str1) then
>     // Create Directory Str1
>     // call to 'mkdir' ??? what about on Linux??? and other platforms?
>
> What is the cross-platform way for me to create the directory in Str1 (as returned by
> GetAppConfigDir) ?
use ForceDirectories from the Sysutils unit:

http://www.freepascal.org/docs-html/rtl/sysutils/forcedirectories.html

Michael.
--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

Re: [Lazarus] How to create a directory from a path string? (cross-platform needed).

silvioprog
In reply to this post by silvioprog
Oops,

2011/3/15 silvioprog <[hidden email]>:
> 2011/3/15 Peter Williams <[hidden email]>:
> (...)
> var
>   Str1 : string;
> begin
>  Str1 := GetAppConfigDir(False);
>   if not DirectoryExists(Str1) then
  // ForceDirectory(Str1);
  ForceDirectories(Str1);

^^'.

> (...)
>
> Good luck. ;)

--
Silvio Clécio
=================================================
Blog - silvioprog.com.br
Twitter - twitter.com/silvioprog
LazSolutions - code.google.com/p/lazsolutions
Lazarus-BR - http://groups.google.com.br/group/lazarus-br?hl=pt-BR
=================================================

--
_______________________________________________
Lazarus mailing list
[hidden email]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
12