Scripting FileSystemObject-读取Unicode文件
经典ASP,VBScript上下文 很多文章,比如说你不能使用FileSystemObject来读取Unicode文件 前一段时间我遇到了这个问题,所以根据ReadText示例,我改为使用ADODB.Stream,而不是使用(它接受最后一个参数,指示是否以unicode打开文件,但实际上不起作用) 然而,ADODB.Stream在尝试读取UNC文件共享(权限相关问题)上的文件时会带来痛苦。因此,在研究这一点时,我偶然发现了以下方法:a)使用unicode文件,b)使用UNC文件共享:Scripting FileSystemObject-读取Unicode文件,scripting,asp-classic,vbscript,Scripting,Asp Classic,Vbscript,经典ASP,VBScript上下文 很多文章,比如说你不能使用FileSystemObject来读取Unicode文件 前一段时间我遇到了这个问题,所以根据ReadText示例,我改为使用ADODB.Stream,而不是使用(它接受最后一个参数,指示是否以unicode打开文件,但实际上不起作用) 然而,ADODB.Stream在尝试读取UNC文件共享(权限相关问题)上的文件时会带来痛苦。因此,在研究这一点时,我偶然发现了以下方法:a)使用unicode文件,b)使用UNC文件共享: dim f
dim fso, file, stream
set fso = Server.CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile("\\SomeServer\Somefile.txt")
set stream = file.OpenAsTextStream(ForReading,-1) '-1 = unicode
这是在使用FSO读取unicode文件时没有任何明显的问题,因此我对所有引用(包括MS)都感到困惑,说您不能使用FSO读取unicode文件
还有其他人使用这种方法读取unicode文件吗?是否有我遗漏的任何隐藏的陷阱,或者您真的可以使用FSO读取unicode文件?我想说,如果它有效,请使用它;-)
我注意到您提到的MS文章来自Windows 2000(!)脚本指南。也许它已经过时了。是的,文档已经过时了。脚本组件在早期确实经历了一系列更改(如果使用早期绑定,其中一些更改会破坏更改),但至少从WK2000 SP4和XP SP2开始,它就非常稳定
只要小心你所说的unicode是什么意思。有时unicode一词的使用范围更广,可以涵盖unicode的任何编码。例如,FSO不读取unicode的UTF8编码。为此,您需要求助于ADODB.Stream。我认为微软没有正式声明它支持unicode,因为:
我正在编写一个Windows7小工具,遇到了同样的问题,如果可能的话,您可以将文件切换到另一种编码,例如:ANSI编码“windows-1251”。使用这种编码,它工作得很好 如果您正在使用它来编写站点,那么最好使用另一种开发方法来避免此对象。
'假设我们检测到它是Unicode文件,那么就非常简单了
'assume we have detected that it is Unicode file - then very straightforward
'byte-by-byte crawling sorted out my problem:
'.
'.
'.
else
eilute=f.ReadAll
'response.write("ČIA BUVO ČARLIS<br/>")
'response.write(len(eilute))
'response.write("<br/>")
elt=""
smbl=""
for i=3 to len(eilute) 'First 2 bytes are 255 and 254
baitas=asc(mid(eilute,i,1))
if (i+1) <= len(eilute) then
i=i+1
else
exit for
end if
antras=asc(mid(eilute,i,1))*256 ' raidems uzteks
'response.write(baitas)
'response.write(asc(mid(eilute,i,1)))
'response.write("<br/>")
if baitas=13 and antras=0 then 'LineFeed
response.write(elt)
response.write("<br/>")
elt=""
if (i+2) <= len(eilute) then i=i+2 'persokam per CarriageReturn
else
skaicius=antras+baitas
smbl="&#" & skaicius & ";"
elt=elt & smbl
end if
next
if elt<>"" then
response.write(elt)
response.write("<br/>")
elt=""
end if
end if
f.Close
'.
'.
'逐字节爬行解决了我的问题:
'.
'.
'.
其他的
eilute=f.ReadAll
“回复。写(Č;IA BUVOČ;ARLIS
”)
'响应.写入(len(eilute))
'response.write(“
”)
elt=“”
smbl=“”
对于i=3到len(eilute),前2个字节是255和254
baitas=asc(中期(埃鲁特,i,1))
如果(i+1)谢谢。在本例中,被读取为“unicode”的文件都是由类似的代码创建的,这些代码使用FSO.OpenTextFile(TriStateTrue表示“unicode”)打开文件进行写入,因此使用FSO读取所有文件应该是安全的。Stream在试图读取另一台机器上的文件共享时会导致各种各样的hoo-haa,这就是为什么我不再这样做的原因。
'assume we have detected that it is Unicode file - then very straightforward
'byte-by-byte crawling sorted out my problem:
'.
'.
'.
else
eilute=f.ReadAll
'response.write("ČIA BUVO ČARLIS<br/>")
'response.write(len(eilute))
'response.write("<br/>")
elt=""
smbl=""
for i=3 to len(eilute) 'First 2 bytes are 255 and 254
baitas=asc(mid(eilute,i,1))
if (i+1) <= len(eilute) then
i=i+1
else
exit for
end if
antras=asc(mid(eilute,i,1))*256 ' raidems uzteks
'response.write(baitas)
'response.write(asc(mid(eilute,i,1)))
'response.write("<br/>")
if baitas=13 and antras=0 then 'LineFeed
response.write(elt)
response.write("<br/>")
elt=""
if (i+2) <= len(eilute) then i=i+2 'persokam per CarriageReturn
else
skaicius=antras+baitas
smbl="&#" & skaicius & ";"
elt=elt & smbl
end if
next
if elt<>"" then
response.write(elt)
response.write("<br/>")
elt=""
end if
end if
f.Close
'.
'.