vb读写ini文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb读写ini文件相关的知识,希望对你有一定的参考价值。
请问大侠们:vb中如何读写ini文件,急盼详细语句,并逐句注释详解,万分感谢!!
两种方法1、API
[名称] 读写INI文件的四个函数
[数据来源] 自己制作
[来源时间] 2002.01.04
[内容简介] 空
[源代码内容]
'文件名SourceDB.ini文件
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal
lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal
lpString As Any, ByVal lpFileName As String) As Long
'以下两个函数,读/写ini文件,固定节点setting,in_key为写入/读取的主键
'仅仅针对是非值
'Y:yes,N:no,E:error
Public Function GetIniTF(ByVal In_Key As String) As Boolean
On Error GoTo GetIniTFErr
GetIniTF = True
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString "Setting", In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "1" Then
GetIniTF = True
GetStr = ""
Else
GoTo GetIniTFErr
End If
Exit Function
GetIniTFErr:
Err.Clear
GetIniTF = False
GetStr = ""
End Function
Public Function WriteIniTF(ByVal In_Key As String, ByVal In_Data As Boolean) As Boolean
On Error GoTo WriteIniTFErr
WriteIniTF = True
If In_Data = True Then
WritePrivateProfileString "Setting", In_Key, "1", App.Path & "\SourceDB.ini"
Else
WritePrivateProfileString "Setting", In_Key, "0", App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniTFErr:
Err.Clear
WriteIniTF = False
End Function
'以下两个函数,读/写ini文件,不固定节点,in_key为写入/读取的主键
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
以上代码保存于: SourceCode Explorer(源代码数据库)
复制时间: 2007.12.26 17:29:09
软件版本: 1.0.882
软件作者: Shawls
E-Mail: ShawFile@163.com
QQ: 9181729
2、用open打开文件全部读入,如果有修改覆盖写入文件 参考技术A 我以前整理的VB读取\修改\写入ini文件的方法,应该有用 但是没有注释
读:
已知:在D盘有有一个名为test.ini的文件,内容如下
==========
[Live]
live1=41
live2=46
live3=54
==========
在Form1中有text1,text2和text3三个控件。
要求:在Form1运行时读取test.ini,在text1中显示live1中的内容,在text2中显示live2中的内容,在text3中显示live3中的内容。
代码:
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Function GetFromIni(strSectionHeader As String, strVariableName As String, strFileName As String) As String
Dim strReturn As String
strReturn = String(255, Chr(0))
GetFromIni = Left$(strReturn, GetPrivateProfileString(strSectionHeader, ByVal strVariableName, "", strReturn, Len(strReturn), strFileName))
End Function
Private Sub Form_Load()
Text1.Text = GetFromIni("Live", "live1","D:/test.ini")
Text2.Text = GetFromIni("Live", "live2","D:/test.ini")
Text3.Text = GetFromIni("Live", "live3","D:/test.ini")
End Sub
改:
已知:D盘有一个名为test.ini的文件,内容有一部分是这样的:
[live]
boolean=1
要把1改为0,代码如下
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Function SetIniValue(FileName As String, ByVal clsName As String, ByVal key As String, Optional ByVal Value As String = "")
SetIniValue = WritePrivateProfileString(clsName, key, Value, FileName)
End Function
Private Sub Command1_Click()
Dim FileName As String
Dim str As String
FileName = "D:\test.ini(路径)"
str = "0(等号后的内容)"
SetIniValue FileName, "live(标签)", "boolean(等号前的内容)", str
End Sub
写:
已知:C盘有一个空的名为config.ini的文件,Form1上有Command1,text1和text2。text1内容为"abcdefg",text2可供用户随意修改。
要求:在点击Command1时把text1和text2中的内容写入config.ini,要求写入后的ini内容如下:
==========
[name]
username=abcdefg
username2=【text2中的内容】
==========
代码:
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Sub Command1_Click()
WritePrivateProfileString "name" ,"username","abcdefg","c:\config.ini"
WritePrivateProfileString "name" ,"username2",text2.text,"c:\config.ini"
End Sub
C# 读写Ini文件
ini文件在Win95以前比较盛行,之后由于出册表等技术的出现,ini技术主键退居二线,不过对于一些小项目,读写ini文件还是很适用的。
Windows API提供了读写配置文件的操作,在C#程序中只要导入相应的API即可。例如GetPrivateProfileString()方法,在MSDN查得原型如下:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpDefault, // default string
LPTSTR lpReturnedString, // destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // initialization file name
);
由于C#和C++数据类型不同,在导入win32 API时需要做相应的转换,并且要导入相关的类。如下所示:
[ ]
private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
同理导入另一个函数
[ ]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath)
在main函数测试如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace DoingIni
{
class Program
{
[ ]
private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
[ ]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">项目名称(如 [section] )</param>
/// <param name="skey">键</param>
/// <param name="path">路径</param>
static string IniReadValue(string section, string skey, string path)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(section, skey, "", temp, 500, path);
return temp.ToString();
}
/// <summary>
/// 写入ini文件
/// </summary>
/// <param name="section">项目名称</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="path">路径</param>
static void IniWrite(string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}
static void Main(string[] args)
{
IniWrite("上海", "东方明珠", "浦东新区", "f:/test.ini");
IniWrite("上海", "上海中心大厦", "浦东新区", "f:/test.ini");
IniWrite("湖南", "张家界景区", "张家界", "f:/test.ini");
IniWrite("湖南", "凤凰古城", "吉首", "f:/test.ini");
IniWrite("湖北", "武当山", "十堰", "f:/test.ini");
IniWrite("湖北", "黄鹤楼", "武昌", "f:/test.ini");
IniWrite("湖北", "古隆中", "襄阳", "f:/test.ini");
IniWrite("江苏", "瘦西湖", "扬州", "f:/test.ini");
IniWrite("江苏", "中山陵", "南京", "f:/test.ini");
IniWrite("上海", "城隍庙", "浦东新区", "f:/test.ini");
string str = IniReadValue("湖南", "凤凰古城", "f:/test.ini");
Console.WriteLine(str);
IniReadValue("上海", null, "f:/test.ini");
删除test.ini文件中A 段落中所有键(没有键,也就没有值了)
//IniWrite("A", null, null, "f:/test.ini");
删除test.ini文件中所有段落
//IniWrite(null, null, null, "f:/test.ini");
Console.ReadKey();
}
}
}
结果如下:
以上是关于vb读写ini文件的主要内容,如果未能解决你的问题,请参考以下文章