Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-03

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

25 messages · 13 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110315/158dcc44/attachment.html>

Reply by email

ik
to Peter Williams

2011/3/15 Peter Williams

Show quoted text 61 lines
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 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110315/7c670a81/attachment-0002.html>

Reply by email

zeljko
to Peter Williams: I use this procedure to save an inifile using the standard IniFiles unit. The problem I…
Peter Williams wrote 9 lines
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

Reply by email

Lukasz Sokol
to Peter Williams: I use this procedure to save an inifile using the standard IniFiles unit. The problem I…
Peter Williams wrote 12 lines
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.

Reply by email

Bart
to Peter Williams: I use this procedure to save an inifile using the standard IniFiles unit. The problem I…
Peter Williams 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

Reply by email

Sven Barth
to Bart
Bart wrote 10 lines
On 3/15/11, Peter Williams 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

Reply by email

Hans-Peter Diettrich
to Sven Barth
Show quoted text 8 lines
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

Reply by email

Peter Williams
to Bart: You really should not do that in most OS's, even on Windows it is deprecated since win9x.…

Hi Bart,

Show quoted text 15 lines
Date: Tue, 15 Mar 2011 12:45:49 +0100 From: To: 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 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;

Show quoted text 8 lines
Bart -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110316/c182b7a6/attachment-0001.html>

Reply by email

Sven Barth
to Peter Williams
Peter Williams wrote 23 lines
Hi Bart, > Date: Tue, 15 Mar 2011 12:45:49 +0100 > From: > To: > 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 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).

Show quoted text 6 lines
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

Reply by email

Peter Williams
to Peter Williams: I found the declaration for function GetAppConfigDir in osutilsh.inc but I cannot find…

Hi,

From:
To:
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,

Show quoted text 15 lines
Date: Tue, 15 Mar 2011 12:45:49 +0100 From: To: 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 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;

Show quoted text 8 lines
Bart -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Sven Barth
to Peter Williams
Peter Williams wrote 7 lines
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

Reply by email

Peter Williams
to Sven Barth

Hi Sven,

Show quoted text 16 lines
Date: Tue, 15 Mar 2011 16:02:55 +0100 From: To: 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?
Best Regards,PEW

Show quoted text 9 lines
Regards, Sven -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110316/1ed4ace4/attachment.html>

Reply by email

leledumbo
to Peter Williams: I use this procedure to save an inifile using the standard IniFiles unit. The problem I…

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 by email

Henry Vermaak
to Peter Williams: var str1 : string;begin str1 := GetAppConfigDir( true ); ShowMessage( Str1 ); //…

2011/3/15 Peter Williams :

Show quoted text 37 lines
Hi Sven, > Date: Tue, 15 Mar 2011 16:02:55 +0100 > From: > To: > 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

Reply by email

silvioprog
to Peter Williams: var str1 : string;begin str1 := GetAppConfigDir( true ); ShowMessage( Str1 ); //…
2011/3/15 Peter Williams :
(...)
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 =================================================

Reply by email

William Oliveira Ferreira
to silvioprog

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

Show quoted text 27 lines
2011/3/15 Peter Williams : (...) 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://code.google.com/p/lazsolutionsLazarus-BR> - http://groups.google.com.br/group/lazarus-br?hl=pt-BR ================================================= -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110315/484a3729/attachment-0001.html>

Reply by email

silvioprog
to William Oliveira Ferreira

2011/3/15 William Oliveira Ferreira :

Show quoted text 5 lines
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.

Show quoted text 13 lines
2011/3/15 silvioprog > > 2011/3/15 Peter Williams : > (...) > 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 =================================================

Reply by email

Michael Van Canneyt
to Peter Williams: var str1 : string;begin str1 := GetAppConfigDir( true ); ShowMessage( Str1 ); //…
Peter Williams wrote 31 lines
Hi Sven, > Date: Tue, 15 Mar 2011 16:02:55 +0100 > From: > To: > 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.

Reply by email

silvioprog
to silvioprog: 2011/3/15 Peter Williams : (...) var ?Str1 : string; begin Str1 := GetAppConfigDir(False);

Oops,

2011/3/15 silvioprog :

Show quoted text 7 lines
2011/3/15 Peter Williams : (...) 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 =================================================

Reply by email

William Oliveira Ferreira
to silvioprog: 2011/3/15 William Oliveira Ferreira : CreateDir does not create recursive path. Silvio…

Interestingly, this detail about ForceDirectory I did not know ________________________________
William de Oliveira Ferreira
Bacharel em Sistemas de Informa??o

2011/3/15 silvioprog

Show quoted text 39 lines
2011/3/15 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 CreateDir does not create recursive path. > 2011/3/15 silvioprog >> >> 2011/3/15 Peter Williams : >> (...) >> 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://code.google.com/p/lazsolutionsLazarus-BR> - http://groups.google.com.br/group/lazarus-br?hl=pt-BR ================================================= -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20110315/b58ba240/attachment.html>

Reply by email

Sven Barth
to Peter Williams: var str1 : string;begin str1 := GetAppConfigDir( true ); ShowMessage( Str1 ); //…
Peter Williams wrote 44 lines
Hi Sven, > Date: Tue, 15 Mar 2011 16:02:55 +0100 > From: > To: > 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?

While the others already sent you the correct solution ("ForceDirectories") I just wanted to note, that FPC abstracts all those file and directory related functions for you. There are for example the very low level functions mkdir and rmdir that are part of the System unit.

See here: http://www.freepascal.org/docs-html/rtl/system/mkdir.html

Then there are the functions defined in SysUtils like CreateDir, ForceDirectories and RemoveDir.

See here: http://www.freepascal.org/docs-html/rtl/sysutils/createdir.html

And recent Delphi versions also include a higher level by introducing advanced record types that handle all this:
http://docwiki.embarcadero.com/VCL/en/IOUtils.TDirectory

Regards,
Sven

Reply by email

Bart
to leledumbo: Weird, I never had such a problem. In fact, one of my application uses it (the others use…

It is perfectly normal to open and change an existing ini file:

Program Test;
{$mode objfpc}
{$h+}
uses sysutils, inifiles;
var
  F: TIniFile;
  Path: String;
  Value1, Value2: String;
begin
  Path := 'test.ini';
  F := TIniFile.Create(Path);
  if ParamCount > 0 then Value1 := ParamStr(1) else Value1 := 'Value_1';
  if ParamCount > 1 then Value2 := ParamStr(2) else Value2 := 'Value_2';
  Try
    F.WriteString('Section','Item_1',Value1);
    F.WriteString('Section','Item_2',Value2);
  Finally
    F.Free;
  end;
end.

bart at simenon:~/LazarusProjecten/ConsoleProjecten> fpc test.pp Free Pascal Compiler version 2.4.2 [2010/11/08] for i386 Copyright (c) 1993-2010 by Florian Klaempfl
Target OS: Linux for i386
Compiling test.pp
Linking test
23 lines compiled, 0.8 sec
bart at simenon:~/LazarusProjecten/ConsoleProjecten> ./test bart at simenon:~/LazarusProjecten/ConsoleProjecten> cat test.ini [Section]
Item_1=Value_1
Item_2=Value_2

bart at simenon:~/LazarusProjecten/ConsoleProjecten> ./test One Two bart at simenon:~/LazarusProjecten/ConsoleProjecten> cat test.ini [Section]
Item_1=One
Item_2=Two

Now I make the file test.ini read-only:

bart at simenon:~/LazarusProjecten/ConsoleProjecten> chmod -w test.ini bart at simenon:~/LazarusProjecten/ConsoleProjecten> ./test One_One Two_Two An unhandled exception occurred at $08082182 :
EFCreateError : Unable to create file "test.ini"
$08082182
$0808A223
$0807AC8E
$0807AD48
$080481F3

And, as expected, creating the file (with write permissions), fails.

And, changing it back again to read-write access resolves the problem:

bart at simenon:~/LazarusProjecten/ConsoleProjecten> chmod +w test.ini bart at simenon:~/LazarusProjecten/ConsoleProjecten> ./test One_One Two_Two bart at simenon:~/LazarusProjecten/ConsoleProjecten> cat test.ini [Section]
Item_1=One_One
Item_2=Two_Two

Bart

leledumbo wrote 17 lines
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. -- View this message in context: http://free-pascal-lazarus.989080.n3.nabble.com/Lazarus-Unable-to-rewrite-ini-file-which-exists-error-unable-to-create-ini-file-need-to-rewrite-it-tp2680589p2681987.html Sent from the Free Pascal - Lazarus mailing list . -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Marco van de Voort
to Sven Barth: While the others already sent you the correct solution ("ForceDirectories") I just wanted…
Sven Barth wrote 13 lines
> explain. > > An example, please? While the others already sent you the correct solution ("ForceDirectories") I just wanted to note, that FPC abstracts all those file and directory related functions for you. There are for example the very low level functions mkdir and rmdir that are part of the System unit. See here: http://www.freepascal.org/docs-html/rtl/system/mkdir.html Then there are the functions defined in SysUtils like CreateDir, ForceDirectories and RemoveDir.

Note that for some cases, security aware programs (CGI, daemons), you have to be a bit more careful. A known dangerous case is stuff like forcedirectories on directory hierarchies where you don't have read permissions (but you do have "x" permissions) on all levels.

And recent Delphi versions also include a higher level by introducing advanced record types that handle all this: http://docwiki.embarcadero.com/VCL/en/IOUtils.TDirectory

Fashion, not function. .NET has everything organized in static classes, and embacadero wants to look more modern.

I wouldn't call this higher level. It is the same level just in OOP style.

Reply by email

Sven Barth
to Marco van de Voort
Marco van de Voort wrote 20 lines
On Wed, Mar 16, 2011 at 09:39:38AM +0100, Sven Barth wrote: >> explain. >> >> An example, please? > > While the others already sent you the correct solution > ("ForceDirectories") I just wanted to note, that FPC abstracts all those > file and directory related functions for you. There are for example the > very low level functions mkdir and rmdir that are part of the System unit. > > See here: http://www.freepascal.org/docs-html/rtl/system/mkdir.html > > Then there are the functions defined in SysUtils like CreateDir, > ForceDirectories and RemoveDir. Note that for some cases, security aware programs (CGI, daemons), you have to be a bit more careful. A known dangerous case is stuff like forcedirectories on directory hierarchies where you don't have read permissions (but you do have "x" permissions) on all levels.

Outch... That could indeed be dangerous.

Show quoted text 7 lines
> And recent Delphi versions also include a higher level by introducing > advanced record types that handle all this: > http://docwiki.embarcadero.com/VCL/en/IOUtils.TDirectory Fashion, not function. .NET has everything organized in static classes, and embacadero wants to look more modern.

They also want the language to look more like .NET (e.g. the attributes syntax). *sigh* (or in other words: they take the cheap way by reusing the Delphi.NET parser...)

I wouldn't call this higher level. It is the same level just in OOP style.

Ok... fine as well. :)

Regards,
Sven

Reply by email