Recent

Author Topic: CreateDir not work.  (Read 3391 times)

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
CreateDir not work.
« on: June 04, 2015, 05:09:49 pm »
Here i try to recreate my problem in a simple example, is a typically unicode problem but i not find a solution, occurs only in Windows when i try to get user folder and create a new folder inside it but is not soo simple
consider the user name 'Niño López' and the foldor is c:\documents and settings\Niño López\

here is the example code:
Code: [Select]
procedure example();
var
    userdir:string;
    {$IFDEF WINDOWS}
      registro:TRegistry;
    {$ENDIF}
begin
  userdir:=UTF8ToSys(GetUserDir()+'New Folder');
      {$IFDEF WINDOWS}
        registro:=TRegistry.Create;
        registro.RootKey:=HKEY_CURRENT_USER;
        registro.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\',false);
        userdir:=SysToUTF8(registro.ReadString('Personal')+PathDelim+'New Folder');
        registro.CloseKey;
        registro.Free;
      {$ENDIF}   
     Form2.Edit1.Text:=userdir;          // The user can modify the userdir after create it
     Form2.ShowModal;
     if not DirectoryExists(Form2.Edit1.Text) then
        CreateDir(UTF8ToSys(Form2.Edit1.Text));
end;
This work good in FPC 2.6.4 but not in new versions, the 'userdir' is represented with rare characters or is not create, any solution?
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

Mando

  • Full Member
  • ***
  • Posts: 181
Re: CreateDir not work.
« Reply #1 on: June 04, 2015, 05:42:22 pm »
Hi:

Try to chage this:

Code: [Select]
if not DirectoryExists(Form2.Edit1.Text) then
        CreateDir(UTF8ToSys(Form2.Edit1.Text));

by this:

Code: [Select]
if not DirectoryExists(UTF8ToSys(Form2.Edit1.Text)) then
        CreateDir(UTF8ToSys(Form2.Edit1.Text));



I have not tried, but I think it will work.

regards

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Re: CreateDir not work.
« Reply #2 on: June 04, 2015, 06:17:28 pm »
This not work if you do that, assuming that the folder exists:
Code: [Select]
if not DirectoryExists(UTF8ToSys(Form2.Edit1.Text)) then
begin
        ShowMessage('Folder not exists creating...');
        CreateDir(UTF8ToSys(Form2.Edit1.Text));
end;
the message is showing, but with this:
if not DirectoryExists(Form2.Edit1.Text) then
begin
        ShowMessage('Folder not exists creating...');
        CreateDir(UTF8ToSys(Form2.Edit1.Text));
end;
[/code]
no message is not show, but  forgot the comprobation, assuming that only CreateDir are execute, is not work to :(.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

paweld

  • Hero Member
  • *****
  • Posts: 1677
Re: CreateDir not work.
« Reply #3 on: June 04, 2015, 06:41:15 pm »
try
Code: [Select]
if not DirectoryExistsUTF8(Form2.Edit1.Text) then
        ForceDirectories(UTF8ToSys(Form2.Edit1.Text));
Best regards / Pozdrawiam
paweld

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Re: CreateDir not work.
« Reply #4 on: June 04, 2015, 07:25:04 pm »
Yes this maybe work, the FPC team write this methods to support unicode (not present in FPC 2.6.4), but not exists a "GetUserDirUTF8" or "TRegistry.ReadStringUTF8" so, exist a lost of information before create the new folder, when you obtain the user folder:
Code: [Select]
{$IFDEF WINDOWS}
        registro:=TRegistry.Create;
        registro.RootKey:=HKEY_CURRENT_USER;
        registro.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\',false);
        userdir:=SysToUTF8(registro.ReadString('Personal')+PathDelim+'New Folder');
        registro.CloseKey;
        registro.Free;
      {$ENDIF}   
     Form2.Edit1.Text:=userdir;
in this point the edit text not show the ó and Ñ characters  :(
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

paweld

  • Hero Member
  • *****
  • Posts: 1677
Re: CreateDir not work.
« Reply #5 on: June 04, 2015, 09:01:54 pm »
Code: [Select]
procedure example;
var
  userdir: string;
begin
  userdir:=SysToUTF8(GetUserDir())+'New Folder';
  Form2.Edit1.Text:=userdir;
  Form2.ShowModal;
  if not DirectoryExistsUTF8(Form2.Edit1.Text) then
  ForceDirectories(UTF8ToSys(Form2.Edit1.Text));
end;   
Best regards / Pozdrawiam
paweld

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Re: CreateDir not work.
« Reply #6 on: June 06, 2015, 02:57:50 pm »
Very thanks paweld now is work i test it on varios systems :).
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

 

TinyPortal © 2005-2018