FileSystemObject - 读取 Unicode 文件 [英] FileSystemObject - Reading Unicode Files

查看:10
本文介绍了FileSystemObject - 读取 Unicode 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经典 ASP、VBScript 上下文.

Classic ASP, VBScript context.

很多文章,包括 这个微软的,假设您不能使用 FileSystemObject 来读取 Unicode 文件.

A lot of articles including this Microsoft one, say you cannot use FileSystemObject to read Unicode files.

我不久前遇到了这个问题,因此根据 ReadText 示例切换到使用 ADODB.Stream 这里,而不是使用 FileSystemObject.OpenTextFile(它接受一个最后的参数,表明是否以 unicode 格式打开文件,但实际上不起作用).

I encountered this issue a while back, so switched to using ADODB.Stream instead, per the ReadText example here, instead of using FileSystemObject.OpenTextFile (which does accept a final parameter indicating whether to open the file as unicode, but actually doesn't work).

但是,当尝试读取 UNC 文件共享上的文件时,ADODB.Stream 会带来麻烦(与权限相关的问题).因此,在对此进行调查时,我偶然发现了以下方法,它适用于 a) unicode 文件,b) 跨 UNC 文件共享:

However, ADODB.Stream results in a world of pain when trying to read a file on a UNC fileshare (permissions-related issue). So, investigating this, I stumbled across the following approach which works a) with unicode files, and b) across UNC fileshares:

dim fso, file, stream
set fso = Server.CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile("\SomeServerSomefile.txt")
set stream = file.OpenAsTextStream(ForReading,-1) '-1 = unicode

这是使用 FSO 读取 unicode 文件而没有任何明显问题,所以我对所有参考资料(包括 MS)感到困惑,说您不能使用 FSO 读取 unicode 文件.

This is using the FSO to read a unicode file without any apparent problem, so I'm confused as to all the references, including MS, saying you can't use the FSO to read unicode files.

还有其他人使用这种方法来读取 unicode 文件吗?有没有我遗漏的隐藏问题,或者您真的可以使用 FSO 读取 unicode 文件吗?

Has anyone else used this approach for reading unicode files? Are there any hidden gotchas I'm missing, or can you really actually read unicode files using FSO?

推荐答案

是的,文档已过期.脚本组件在其早期确实经历了一系列更改(如果您使用早期绑定,其中一些更改是重大更改),但是至少从 WK2000 SP4 和 XP SP2 开始,它一直非常稳定.

Yes that documentation is out of date. The scripting component did go through a set of changes in its early days (some of them were breaking changes if you were using early binding) however since at least WK2000 SP4 and XP SP2 it has been very stable.

请注意 unicode 的含义.有时 unicode 这个词的使用范围更广,可以涵盖任何 unicode 编码.FSO 不读取例如 Unicode 的 UTF8 编码.为此,您需要使用 ADODB.Stream.

Just be careful what you mean by unicode. Sometimes the word unicode is used more broadly and can cover any encoding of unicode. FSO does not read for example UTF8 encodings of unicode. For that you would need to fall back on ADODB.Stream.

这篇关于FileSystemObject - 读取 Unicode 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆