Recent

Author Topic: Dealing with Unicode Filenames  (Read 7407 times)

Harrison

  • New Member
  • *
  • Posts: 16
Dealing with Unicode Filenames
« on: June 18, 2010, 02:51:52 pm »
Alright, I've found this thread and this page, and neither seem to be helping me at all.

From what I've read, if a filename contains a unicode character, it needs to be converted to UTF-8. This is an example of how my code was before:

if OpenDialog.Execute then
  begin
    LoadedFile := TMemoryStream.Create;

    LoadedFile.LoadFromFile(OpenDialog.FileName);
  end;

Of course, when loading a file with a unicode character in its name, it would error and say that it could not be opened. So I tried this:

if OpenDialog.Execute then
  begin
    LoadedFile := TMemoryStream.Create;

    LoadedFile.LoadFromFile(SysToUTF8(OpenDialog.FileName));
  end;

But it still errored. I also tried AnsiToUTF8, and as a last ditch effort, ExpandFileNameUTF8, but they all resulted in the same error.

So, what am I missing? From what I'm reading, this should be right, shouldn't it?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Dealing with Unicode Filenames
« Reply #1 on: June 18, 2010, 02:58:01 pm »
UTF8ToSys

Harrison

  • New Member
  • *
  • Posts: 16
Re: Dealing with Unicode Filenames
« Reply #2 on: June 18, 2010, 03:00:35 pm »
 %) That worked. Mind explaining to me why it seems to be backwards than what the page is saying?

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: Dealing with Unicode Filenames
« Reply #3 on: June 18, 2010, 04:20:49 pm »
%) That worked. Mind explaining to me why it seems to be backwards than what the page is saying?

It's not. It's saying:

Quote
Lazarus controls and functions expect filenames and directory names in utf-8 encoding, but the RTL uses ansi strings for directories and filenames.

You get the name from a lazarus control (OpenDialog) in UTF8 and pass it to a RTL Function.
So UTF8ToSys is needed.

 

TinyPortal © 2005-2018