读取和写入Windows的INI文件
代码如下:
Sub WriteINIStringVirtual(Section, KeyName, value, FileName)
WriteINIString Section, KeyName, value, _
Server.MapPath(FileName)
End Sub
Function GetINIStringVirtual(Section, KeyName, Default, FileName)
GetINIStringVirtual = GetINIString(Section, KeyName, Default, _
Server.MapPath(FileName))
End Function
'Work with INI files In VBS (ASP/WSH)
'v1.00
'Function GetINIString(Section, KeyName, Default, FileName)
'Sub WriteINIString(Section, KeyName, value, FileName)
Sub WriteINIString(Section, KeyName, value, FileName)
Dim INIContents, PosSection, PosEndSection
'Get contents of the INI file As a string
INIContents = GetFile(FileName)
'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection > 0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents) + 1
'Separate section contents
Dim OldsContents, NewsContents, Line
Dim sKeyName, Found
OldsContents = Mid$(INIContents, PosSection, PosEndSection - PosSection)
OldsContents = Split(OldsContents, vbCrLf)
'Temp variable To find a Key
sKeyName = LCase$(KeyName & "=")
'Enumerate section lines
For Each Line In OldsContents
If LCase$(Left$(Line, Len(sKeyName))) = sKeyName Then
Line = KeyName & "=" & value
Found = True
End If
NewsContents = NewsContents & Line & vbCrLf
Next
If IsEmpty(Found) Then
'key Not found - add it at the end of section
NewsContents = NewsContents & KeyName & "=" & value
Else
'remove last vbCrLf - the vbCrLf is at PosEndSection
NewsContents = Left$(NewsContents, Len(NewsContents) - 2)
End If
'Combine pre-section, new section And post-section data.
INIContents = Left$(INIContents, PosSection - 1) & _
NewsContents & Mid$(INIContents, PosEndSection)
Else 'if PosSection>0 Then
'Section Not found. Add section data at the end of file contents.
If Right$(INIContents, 2) <> vbCrLf And Len(INIContents) > 0 Then
INIContents = INIContents & vbCrLf
End If
INIContents = INIContents & "[" & Section & "]" & vbCrLf & _
KeyName & "=" & value
End If 'if PosSection>0 Then
WriteFile FileName, INIContents
End Sub
Function GetINIString(Section, KeyName, Default, FileName)
Dim INIContents, PosSection, PosEndSection, sContents, value, Found
'Get contents of the INI file As a string
INIContents = GetFile(FileName)
'Find section
PosSection = InStr(1, INIContents, "[" & Section & "]", vbTextCompare)
If PosSection > 0 Then
'Section exists. Find end of section
PosEndSection = InStr(PosSection, INIContents, vbCrLf & "[")
'?Is this last section?
If PosEndSection = 0 Then PosEndSection = Len(INIContents) + 1
'Separate section contents
sContents = Mid$(INIContents, PosSection, PosEndSection - PosSection)
If InStr(1, sContents, vbCrLf & KeyName & "=", vbTextCompare) > 0 Then
Found = True
'Separate value of a key.
value = SeparateField(sContents, vbCrLf & KeyName & "=", vbCrLf)
End If
End If
If IsEmpty(Found) Then value = Default
GetINIString = value
End Function
'Separates one field between sStart And sEnd
Function SeparateField(ByVal sFrom, ByVal sStart, ByVal sEnd)
Dim PosB
PosB = InStr(1, sFrom, sStart, 1)
If PosB > 0 Then
PosB = PosB + Len(sStart)
Dim PosE
PosE = InStr(PosB, sFrom, sEnd, 1)
If PosE = 0 Then PosE = InStr(PosB, sFrom, vbCrLf, 1)
If PosE = 0 Then PosE = Len(sFrom) + 1
SeparateField = Mid$(sFrom, PosB, PosE - PosB)
End If
End Function
'File functions
Function GetFile(ByVal FileName)
Dim FS
Set FS = CreateObject("Scripting.FileSystemObject")
'Go To windows folder If full path Not specified.
If InStr(FileName, ":") = 0 And Left$(FileName, 2) <> "/" Then
FileName = FS.GetSpecialFolder(0) & "" & FileName
End If
On Error Resume Next
GetFile = FS.OpenTextFile(FileName).ReadAll
End Function
Function WriteFile(ByVal FileName, ByVal Contents)
Dim FS
Set FS = CreateObject("Scripting.FileSystemObject")
'On Error Resume Next
'Go To windows folder If full path Not specified.
If InStr(FileName, ":") = 0 And Left$(FileName, 2) <> "/" Then
FileName = FS.GetSpecialFolder(0) & "" & FileName
End If
Dim OutStream
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function
摘自:网络整理
★VB部分相关文章推荐:★
☆VB6 中善用ByRef 提升速度
☆[vb] Set 语句
☆VB_Format自定义格式
☆用VB如读取内存地址
☆vb FindwindowEx的用法实例
☆进制转化进10进制数
☆收藏:如何获取当前已经打开的IE对象(VB6代码)
☆DXInput中键码的转换(VB6.0代码)
☆如何在VB6.0里动态使用具有事件的对象
☆[vb]格式输出Format函数
☆读取和写入Windows的INI文件
☆简述UTF-8编码原理及其文本文件的读写技术【转】
☆VB中的文件操作
☆VB中的文件操作文档
☆vb 中拷贝文件
☆VB反跟踪技术点滴
☆VB共享软件防破解设计技术初探(二)
☆VB共享软件防破解设计技术初探(三)
☆VB共享软件防破解设计技术初探(一)
☆RTF文件格式【转】
☆VB压缩技术
☆[vb]FSO对象模型在VB中的应用
☆VB 窗体实现文件拖拽获取路径方法
☆VB:注册表的读写
☆vb中空操作(等待)的指令、延时方法
☆VB让控件可以当标题栏拖动
☆FSO对象新建、打开、保存文件
☆获取网关IP和MAC 的VB源码
☆VB文件关联
☆vb获得本地和远程的MAC地址(网卡地址)
☆VB中ShellExeCute的应用
☆VB打开网址方法大全
☆vb简单控制音量大小及静音的方法
☆拖动无边框窗体(VB6代码)
☆VB使用FileSystemObject对象写文件
☆VB 从注册表中删除项及其某个值
☆vb 字符串转为数字和判断字符串是否是数字字符串【转】
☆vb按热键启动应用程序
☆VB的坐标系统综述
☆VB利用API函数来处理文件
☆关于VB中Shell及ShellExecute的总结与记录
☆[vb]On Error GoTo 0和On Error resume区别
☆[vb]On Error 语句
☆记录一下:在菜单上添加自绘图形的例子(VB6代码)
☆vb中findwindow的疑惑
☆[vb]FindWindow使用方法
☆常用文件类[转,无法运行通,待调试]
☆[vb]url utf-8编码
☆VB中的Unicode 和Ansi 格式
☆VB中的format格式化函数
☆VB中字符串匹配的多种方式
☆VB抓图
☆谈vb目录文件操作的三种方法-2
☆谈vb目录文件操作的三种方法-1
☆vb使用open方法读写文件
☆VB的MD5加密模块
☆VB 超简单的屏幕截图代码
☆vb以类名或窗口标题查找句柄并关闭
☆VB将配置保存到EXE本身(生成EXE木马程序)
☆VB 调用腾讯截图控件CameraDLL.dll
☆在VB6.0中怎么实现escape和unescape
☆vb求任意两线交点
☆VB中调用Windows API的注意事项[VB知识库]
☆VB 一个获得自己外网IP 地址的程序代码
☆VB程序中实现IP地址子网掩码网关DNS的更改[转]
☆在VB 中应用FSO 对象模型介绍(摘自网络)
☆[转] Vb中FSO 对象的介绍
☆VB 画坐标轴
☆VB 二进制文件的操作
☆[VB]BMP转JPG
☆VB中KeyCode常数用法
☆vb实时曲线的绘制和保存
更多精彩>>>
读取和写入Windows的INI文件相关推荐
- php 输出tab_php实现读取和写入tab分割的文件
本文实例讲述了php实现读取和写入tab分割的文件.分享给大家供大家参考.具体分析如下: 这段php代码实现读取和写入tab分割的文件,包含两个独立的函数,一个读,一个写,例如cvs文件等 // // ...
- python对txt文本文件边读边写,同时读取和写入的方式修改文件
看到很多人在修改文本文件的时候是先打开一个文本文件并读取,然后关闭该文本文件. 接着再打开一个文本文件用于写入. 但有时候只是想修改文件里面的一些字符,用上面的方法难免感觉有些麻烦. 下面介绍如何一次 ...
- Java读取、写入、处理Excel文件中的数据
在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Excel会有 ...
- 读取配置文件(configparser,.ini文件)
使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [baseconf] host=127.0.0.1 port=3306 user=root p ...
- Python读取,写入,保存txt文件
文件夹1:文件夹2:mnist11.txt 读取的三种方法 方法一: f=open('文件夹1/文件夹2/mnist11.txt') print(f.read()) 方法二: import os.pa ...
- python3从零学习-5.11.2、 aifc - 读取和写入AIFF和aifc文件
源代码:Lib / aifc.py 这个模块提供了对读写AIFF和AIFF- c文件的支持.AIFF是一种音频交换文件格式,用于在文件中存储数字音频样本.AIFF-C是该格式的更新版本,它包含了压缩音 ...
- python读取hdf-eos5数据_python读取与写入csv EXCEK HDF 文件
一. 数据文件 pd指pandas简称,df指DataFrame对象. 1. csv 读取 pd.read_csv('foo.csv') 写入 df.to_csv('foo.csv') 2. HD ...
- html5加js实现本地文件读取和写入并获取本地文件路径
HTML5提供了一台API可以实现文件的读写,文件读取利用API是FileReader 代码如下: 读取本地文件 <!doctype html> <html lang="e ...
- python ui自动化配置文件,Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)...
在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [config ...
最新文章
- 多尺度人脸检测--Face Detection through Scale-Friendly Deep Convolutional Networks
- 对话IT:火狐4.0正式版发布 庆功会上听宫博士“酒后真言”
- windows下nginx安装、配置与使用
- 【C 语言】数组 ( 指针数组用法 | 自我结束能力 )
- 使用ADO.NET操作数据库
- [html] h5页面如何传递参数给小程序?
- 【Javascript】深入理解this作用域问题以及new/let/var/const对this作用域的影响
- linux之man命令
- 11月22日学习内容整理:bootstrap居中处理和组件,常用组件
- 多个注解可以合并成一个,包括自定义注解
- 微信小程序;AI智能配音助手
- Python读取PDF文档并翻译
- AUTOSAR架构深度解析
- Windows登录多微信
- php 五舍六入,四舍五入计算器 四舍五入、四舍六入五取偶(双)算法 - 数学公式 - 房贷计算器...
- SQL Server 2016 COMPRESS 和 DECOMPRESS 函数
- 基于GIS技术的城市交通管理应用
- pace.js网页自动加载进度条插件-好东西
- DeDeCMS v5.7 SP2 前台任意用户密码修改漏洞复现
- vue路由的两种模式:hash与history的区别