带有特殊字符的 Lazarus ListBox SaveToFile 文件名
Lazarus ListBox SaveToFile File Name with special characters
我正在使用 Lazarus (1.5) fpc (3.1.1) 编写面向 Windows XP/7/10 的应用程序。
我的应用程序以用户选择的某些路径在文件系统中读取和写入文件。如果路径或文件名包含特殊字符(如 à è é),例如:
C:\Users\DeAndrè\out.txt
应用程序引发异常:
'EFCreateError' Unable to create file "C:\Users\DeAndrè\out.txt".
可以通过使用单一表单编写一个简单的应用程序来重现此问题:
拖入窗体一个TListBox(ListBox1),两个TButton(Button1和Button2)和一个TSaveDialog(SaveDialog1)。
在Button1的OnClick事件中(仅用于在ListBox1上写入一些数据):
procedure TForm1.Button1Click(Sender: TObject);
begin
// Simple Add Hello to ListBox
ListBox1.Items.Add('Hello '+IntToStr(ListBox1.Items.Count));
end;
在 Button2 的 OnClick 事件中:
procedure TForm1.Button2Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
ListBox1.Items.SaveToFile(SaveDialog1.FileName);
end;
end;
运行 应用程序并单击 "Button1" 几次(只是为了在列表中添加一些单词)然后单击 Button2 并尝试将内容保存到包含特殊字符的路径...
我注意到如果我使用函数 UTF8ToAnsi 转换 FileName 它会起作用,但为什么呢? Windows 文件系统不是 UTF8 吗?
有"standard"解决办法吗?例如将应用程序设置为以正确模式或类似模式使用文件系统?
谢谢
在 Rudy 和 David 的指导下,我找到了解决方案:
要使 Lazarus 能够使用 UnicodeAPI,您必须在 "Custom Option":
上添加 -dEnableUTF8RTL
在"Project"->"Project Options"->"Additions and Override"
单击 "Add"->"Custom Option" 并添加
-dEnableUTF8RTL
这会强制编译器使用 Unicode 进行文件系统访问。
也可以点击按钮 "Set UTF8 in RTL"。
此按钮除了 -dEnableUTF8RTL 之外还添加了选项:
-FcUTF8
在这个 link 的 Lazarus 论坛中:http://forum.lazarus.freepascal.org/index.php?topic=27240.0
"Wiki" 页面中有关于 Lazarus 和 UTF8 的摘录:
Usually the RTL uses the system codepage for strings (e.g. FileExists
and TStringList.LoadFromFile). On Windows this is a non Unicode
encoding, so you can only use characters from your language group. The
LCL works with UTF-8 encoding, which is the full Unicode range. On
Linux and Mac OS X UTF-8 is typically the system codepage, so the RTL
uses here by default CP_UTF8.
Since FPC 2.7.1 the default system codepage of the RTL can be changed
to UTF-8 (CP_UTF8). So Windows users can now use UTF-8 strings in the
RTL.
我正在使用 Lazarus (1.5) fpc (3.1.1) 编写面向 Windows XP/7/10 的应用程序。 我的应用程序以用户选择的某些路径在文件系统中读取和写入文件。如果路径或文件名包含特殊字符(如 à è é),例如:
C:\Users\DeAndrè\out.txt
应用程序引发异常:
'EFCreateError' Unable to create file "C:\Users\DeAndrè\out.txt".
可以通过使用单一表单编写一个简单的应用程序来重现此问题: 拖入窗体一个TListBox(ListBox1),两个TButton(Button1和Button2)和一个TSaveDialog(SaveDialog1)。
在Button1的OnClick事件中(仅用于在ListBox1上写入一些数据):
procedure TForm1.Button1Click(Sender: TObject);
begin
// Simple Add Hello to ListBox
ListBox1.Items.Add('Hello '+IntToStr(ListBox1.Items.Count));
end;
在 Button2 的 OnClick 事件中:
procedure TForm1.Button2Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
ListBox1.Items.SaveToFile(SaveDialog1.FileName);
end;
end;
运行 应用程序并单击 "Button1" 几次(只是为了在列表中添加一些单词)然后单击 Button2 并尝试将内容保存到包含特殊字符的路径...
我注意到如果我使用函数 UTF8ToAnsi 转换 FileName 它会起作用,但为什么呢? Windows 文件系统不是 UTF8 吗?
有"standard"解决办法吗?例如将应用程序设置为以正确模式或类似模式使用文件系统?
谢谢
在 Rudy 和 David 的指导下,我找到了解决方案: 要使 Lazarus 能够使用 UnicodeAPI,您必须在 "Custom Option":
上添加 -dEnableUTF8RTL在"Project"->"Project Options"->"Additions and Override"
单击 "Add"->"Custom Option" 并添加
-dEnableUTF8RTL
这会强制编译器使用 Unicode 进行文件系统访问。
也可以点击按钮 "Set UTF8 in RTL"。 此按钮除了 -dEnableUTF8RTL 之外还添加了选项:
-FcUTF8
在这个 link 的 Lazarus 论坛中:http://forum.lazarus.freepascal.org/index.php?topic=27240.0 "Wiki" 页面中有关于 Lazarus 和 UTF8 的摘录:
Usually the RTL uses the system codepage for strings (e.g. FileExists and TStringList.LoadFromFile). On Windows this is a non Unicode encoding, so you can only use characters from your language group. The LCL works with UTF-8 encoding, which is the full Unicode range. On Linux and Mac OS X UTF-8 is typically the system codepage, so the RTL uses here by default CP_UTF8.
Since FPC 2.7.1 the default system codepage of the RTL can be changed to UTF-8 (CP_UTF8). So Windows users can now use UTF-8 strings in the RTL.