喂。
我需要扫描一个目录及其子文件夹,我使用了FindFirst和FindNext过程,但是TSearchRec的Name属性是一个字符串,因此unicode文件夹名(希伯来语、阿拉伯语等)是‘?’在Name属性中。
我试着在WideFindFirst、WideFindNext和TSearchRecW上使用TntComponent。但我还是得到了?用于文件夹名称。
Flname:=WideExtractFileName(FileSpec);
validres := WideFindFirst(FileSpec+'\*', faDirectory, SearchRec);
AssignFile(LogFile, ResultFilePath);
while validres=0 do begin
if (SearchRec.Attr and faDirectory>0) and (SearchRec.Name[1]<>'.') then begin
{invalid entry Findnext returns}
Append(LogFile);
WriteLn(LogFile, FileSpec+'\'+LowerCase(SearchRec.Name));
CloseFile(LogFile);
DirScan(FileSpec+'\'+SearchRec.Name, ResultFilePath)
end;
validres:=WideFindNext(SearchRec);
end;
WideFindClose(SearchRec);
发布于 2009-01-19 08:59:48
2009年之前的Delphi版本对unicode的支持非常有限。所以如果你真的想要unicode,那么我强烈建议你升级到2009。在2009年,默认字符串是unicode。
你说你在宽版本中仍然有垃圾字符。您是否尝试过使用调试器检查这些值?2009年前的delphi的vcl不能显示unicode字符。
发布于 2009-01-19 10:05:03
Delphi通过使用WideString在编译器中支持unicode。
但您将面临以下问题:
如果你使用原始的unicode windows api,它将会工作。
因此,FindFirst使用FindFirstFile接口,它映射到FindFirstFileA变体,您需要直接调用FindFirstW。
所以你有两个选择。
对于文本文件的编写,您可以使用Primoz Gabrijelcic (又名gabr)的GpTextFile或GpTextSteam,它们支持unicode。
这是一个使用unicode文件名打开文件的示例:
function OpenLongFileName(const ALongFileName: WideString; SharingMode: DWORD): THandle; overload;
begin
if CompareMem(@(WideCharToString(PWideChar(ALongFileName))[1]), @('\\'[1]), 2) then
{ Allready an UNC path }
Result := CreateFileW(PWideChar(ALongFileName), GENERIC_READ, SharingMode, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
else
Result := CreateFileW(PWideChar('\\?\' + ALongFileName), GENERIC_READ, SharingMode, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
end;
function CreateLongFileName(const ALongFileName: WideString; SharingMode: DWORD): THandle; overload;
begin
if CompareMem(@(WideCharToString(PWideChar(ALongFileName))[1]), @('\\'[1]), 2) then
{ Allready an UNC path }
Result := CreateFileW(PWideChar(ALongFileName), GENERIC_WRITE, SharingMode, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
else
Result := CreateFileW(PWideChar('\\?\' + ALongFileName), GENERIC_WRITE, SharingMode, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
end;
我之所以使用这些函数,是因为ANSI api的路径限制为254个字符,unicode的路径限制为2^16个字符。
获得文件的句柄后,只需调用常规的ReadFile delphi api映射,即可从文件中读取数据。
发布于 2009-01-19 09:40:45
一些更新..如果我在SearchRec.Name上使用UTF8Encode,我会得到unicode字符串!下一个问题是我使用的TFileStream。我找不到它的WideString版本(文件名)。
https://stackoverflow.com/questions/456874
复制相似问题