gpt4 book ai didi

windows - 带有特殊字符的 Lazarus ListBox SaveToFile 文件名

转载 作者:可可西里 更新时间:2023-11-01 10:32:55 25 4
gpt4 key购买 nike

我正在使用 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 吗?

有“标准”的解决方案吗?例如将应用程序设置为以正确模式或类似模式使用文件系统?

谢谢

最佳答案

在 Rudy 和 David 的指导下,我找到了解决方案:要使 Lazarus 能够使用 UnicodeAPI,您必须在“自定义选项”中添加 -dEnableUTF8RTL:

在“项目”->“项目选项”->“添加和覆盖”

点击“添加”->“自定义选项”并添加

-dEnableUTF8RTL

这会强制编译器使用 Unicode 进行文件系统访问。

也可以单击“在 RTL 中设置 UTF8”按钮。除了 -dEnableUTF8RTL 之外,此按钮还添加了选项:

-FcUTF8

在 Lazarus 论坛的这个链接中:http://forum.lazarus.freepascal.org/index.php?topic=27240.0关于 Lazarus 和 UTF8 的“Wiki”页面有一段摘录:

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.

关于windows - 带有特殊字符的 Lazarus ListBox SaveToFile 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44024466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com