[QTP]通过vbscript读取一个文件中的所有内容

'作者:席飞剑

'日期:2013-1-23

'功能:读取文件中的所有内容并输出(这是一个很常用的功能)

'参数:txtFile为需要读取的文件,可以根据需要将以下操作封装成一个函数。

'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>实现过程<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Const ForReading = 1, ForWriting = 2
Dim fso, oFile
txtFile="C:\Users\jiffy\Desktop\电台.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(txtFile) Then
Set oFile= fso.OpenTextFile(txtFile, ForReading)

ReadLineTextFile =oFile.ReadLine '读取文件第一行内容
While oFile.AtEndOfStream = false
ReadLineTextFile = ReadLineTextFile & vbNewLine & oFile.ReadLine '遍历文件其它内容
wend
oFile.close() '关闭对象
else
print "File Not Found"
End If

Set fso = nothing '释放对象

msgbox ReadLineTextFile

PS:其它高级编程语言实现此功能方法类似,对于高级程序语言,例如C#,可以通过StreamReader对象进行读取,一直ReadToEnd即可:

string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
StreamReader sr = new StreamReader(FilePath, System.Text.Encoding.Default);
StringBuilder sb = new StringBuilder();
sb.Append(sr.ReadToEnd());
sr.Close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值