文件管理 · 2024年7月22日

读取ini文件|vb 怎么读取ini文件

Ⅰ vb 怎么读取ini文件

为了方便用户使用和使系统具有灵活性,大多数-dows应用程序将用户所做的选择以及各种变化的系统信息记录在初始化(INI)文件中。因此,当系统的环境发生变化时,可以直接修改INI文件,而无需修改程序。由此可见,INI文件对系统功能是至关重要的。本文将介绍采用VisualBasicforWindows(下称VB)开发Windows应用程序时如何读写INI文件。INI文件是文本文件,由若干部分(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,每个关键词会控制应用程序某个功能的工作方式,等号右边的值(value)指定关键词的操作方式。其一般形式如下:[section1]keyword1=valuelkeyword2=value2……[section2]keyword1=value1keyword2=value2……其中,如果等号右边无任何内容(即value为空),那就表示Windows应用程序已为该关键词指定了缺省值,如果在整个文件中找不到某个关键词(或整个一部分),那同样表示为它们指定了缺省值。各个部分所出现的顺序是无关紧要的,在每一个部分里,各个关键词的顺序同样也无关紧要。读写INI文件通常有两种方式:一是在Windows中用"记事本"(Notepad)对其进行编辑,比较简单,无需赘述;二是由Windows应用程序读写INI文件,通常是应用程序运行时读取INI文件中的信息,退出应用程序时保存用户对运行环境的某些修改。关键词的值的类型多为字符串或整数型,应分两种情况读写。为了使程序具有可维护性和可移植性,最好把对INI文件的读写封装在一个模块(RWINI.BAS)中,在RWI-NI.BAS中构造GetIniS和GetIniN函数以及SetIniS和Se-tIniN过程,在这些函数和过程中需要使用WindowsAPI的"GetPrivateprofileString"、"GetPrivateProfileInt"和"WritePrivateProfileString"函数。RWINI.BAS模块的程序代码如下:在General-Declearation部分中声明使用到的WindowsAPI函数:Declare Function GetprivateprofileString Lib"Ker-nel"(ByVallpAppName As String,ByVallpKeyName As String,ByVallpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer,ByVal Filename As String)As IntegerDeclare FunctionGetPrivatePfileInt Lib "Kernel"(ByVal lpAppName As String,ByVal lpKeyName As String,ByVal lpDefault As Integer,ByVal Filename As String)As IntegerDeclare Lib "Kernel"(ByVal lpApplicationName As String,ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As IntegerFunction GetIniS(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefString As String)As StringDim ResultString As String * 144,Temp As IntegerDims As String,i As IntegerTemp%=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName())‘检索关键词的值IfTemp%>0Then‘关键词的值不为空s=""Fori=1To144IfAsc(Mid$(ResultString,I,1))=0ThenExitForElses=s&Mid$(ResultString,I,1)EndIfNextElseTemp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())‘将缺省值写入INI文件s=DefStringEndIfGetIniS=sEndFunctionFunctionGetIniN(ByValSectionNameAsString,ByValKeyWordAsString,ByValDefValueAsIneger)AsIntegerDimdAsLong,sAsStringd=DefValueGetIniN=GetPrivateProfileInt(SectionName,KeyWord,DefValue,ppProfileName())Ifd<>DefValueThens=""&dd=WritePrivateProfileString(SectionName,KeyWord,s,AppProfileName())EndIfEndFunctionSubSetIniS(ByValSectionNameAsString,BtVaKeyWordAsString,ByValValStrAsString)Dimres%res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())EndSubSubSetIniN(ByValSectionNameAsString,ByValKeyWordAsString,ByValValIntAsInteger)Dimres%,s$s$=Str$(ValInt)res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())EndSubSectionName为每一部分的标题,KeyWord为关键词,GetIniS和GetIniN中的DefValue为关键词的缺省值,SetIniS和SetIniN的ValStr和ValInt为要写入INI文件的关键词的值。为了能更好地说明如何使用以上函数和过程,下面举两个实例。实例1:开发应用程序通常要使用数据库和其它一些文件,这些文件的目录(包括路径和文件名)不应在程序中固定,而是保存在INI文件中,程序运行时由INI文件中读入。读入数据库文件的代码如下:DimDatabasenameAsStringDatabasename=GetIniS("数据库","职工","")IfDatabaseName=""ThenDatabaseName=InputBox("请输入数据库《职工》的目录"),App.Title)’也可通过"文件对话框"进行选择OnErrorResumeNextSetdb=OpenDatabas(DatabaseName)IfErr<>0ThenMsgBox"打开数据库失败!",MB-ICONSTOP,App.Title:GotoErrorProcessingElseSetIniS"数据库","职工",DatabaseNameEndIfOnErrorGoTo0……实例2:为了方便用户操作,有时需要保存用户界面的某些信息,例如窗口的高度和宽度等。装载窗体时,从INI文件中读入窗体高度和宽度,卸载窗体时将窗体当前高度和宽度存入INI文件,代码如下:Sub Form1_Load()……Forml.Height=GetIniN("窗体1","高度",6000)Form1.Width=GetIniN("窗体1","高度",4500)EndSub……Sub Form1_Unload()……SetIniN"窗体1","高度",Me.HeightSetIniN"窗体1,"宽度",Me.Width……End Sub

Ⅱ 怎么遍历读取ini文件的所有节点

ini文件读取一般要引入win32API函数来完成,可以参考以下的函数ReadSection,结果以StringCollection的形式返回,你可以自己再做后续处理: public string FileName; //INI文件名 //声明读写INI文件的API函数 [DllImport("kernel32")]

Ⅲ C语言如何新建ini文件,读取ini文件跟写入ini文件

就是fopen()就可以新建文件吧,然后 fgetc(),fgets(),fputc(),fputs(),fseek(),feof(),rewind(),fread(),fwrite()等函数可以完成文件的读写以及定位等,内这些函数都在stdio.h中。例如要容建立一个temp.ini文件的话,就是main(){ FILE *fp; … if((fp=fopen("temp.ini","w"))==NULL) … fclose(fp);}

Ⅳ vc++读取ini文件,根据节点和子节点求出value值,不是用GetPrivateProfileString GetPrivateProfileInt 方

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <windows.h>/*在VC++里面读写配置文件最好还是用GetPrivateProfileString()和GetPrivateProfileInt()函数,如果不用这两个函数,就只有自己写读文件的代码首先建立一个config.ini文件,内容如下[system]server = 192.168.0.1port = 5000[timeout]link = 10000send = 20000recv = 20000*///去掉字符串首尾空格void Trim(char *pStr){ char *pWork = pStr + strlen(pStr) – 1; char *pEnd = pStr + strlen(pStr); while ( pWork >= pStr ) { if ( *pWork == ' ' || *pWork == '\r' || *pWork == '\n' || *pWork == '\t' || *pWork == '\b' ) { memmove(pWork,pWork+1,pEnd-pWork); } pWork–; }}//拆分key = value样式的字符串为key和valuevoid SplitKeyAndValue(char *LineData,char *OutKey,unsigned long KeySize,char *OutValue,unsigned long ValueSize){ char *pWork = LineData; memset(OutKey,0x0,KeySize); memset(OutValue,0x0,ValueSize); while ( *pWork != '=' && pWork <= LineData + strlen(LineData) ) { pWork++; } strncpy(OutKey,LineData,(pWork-LineData) > (KeySize – 1) ? (KeySize – 1) : (pWork-LineData)); Trim(OutKey); strncpy(OutValue, pWork+1, (strlen(LineData) – (pWork-LineData)) > (ValueSize – 1) ? (ValueSize – 1) : (strlen(LineData) – (pWork-LineData))); Trim(OutValue);}//从文件中读取一行数据void ReadLineFromFile(FILE *pf,char *OutLine,unsigned long OutSize){ char tmpC = 0; int index = 0; memset(OutLine,0x0,OutSize); while ( !feof(pf) ) { fread(&tmpC,1,1,pf); if ( tmpC == '\n' ) { OutLine[index] = '\n'; break; } else { OutLine[index] = tmpC; index++; if ( index + 1 >= OutSize ) { OutLine[index] = 0x0; break; } } }}/*char *AppName—结点名称char *KeyName—配置名称char *DefaultValue—-没有指定结点或配置时返回的默认值char *OutStr—接收输出的字符串unsigned long OutStrSize—接收输出字符串的长度char *ConfigFileName—配置文件的名称*/void ReadConfigString(char *AppName,char *KeyName,char *DefaultValue,char *OutStr,unsigned long OutStrSize,char *ConfigFileName = NULL){ if ( NULL == OutStr ) { printf("错误:调用ReadConfigString()函数前必须首先给OutStr分配OutStrSize大小的空间!"); return; } //给返回值赋初值 strncpy(OutStr,DefaultValue,OutStrSize); //获取配置文件名 char RightConfigName[1024]; GetMoleFileNameA(NULL,RightConfigName,1024); char *pWork = RightConfigName + strlen(RightConfigName); if ( NULL == ConfigFileName || strlen(ConfigFileName) < 1 ) { while ( *pWork != '.' ) { pWork–; } strcpy(pWork,".ini"); } else { while ( *pWork != '\\' ) { pWork–; } strcpy(pWork+1,ConfigFileName); } //保存当前结点名称 char CurrentAppName[1024]; memset(CurrentAppName,0x0,1024); //获取结点名称 char RightAppName[1024]; char RightAppNameTmp[1024]; strcpy(RightAppNameTmp,AppName); Trim(RightAppNameTmp); strcpy(RightAppName,"["); strcat(RightAppName,RightAppNameTmp); strcat(RightAppName,"]"); //打开文件读 char ReadLine[1024]; FILE *pf = fopen(RightConfigName,"rt"); if ( NULL == pf ) { perror("打开配置文件错误:"); return; } while( !feof(pf) ) { memset(ReadLine,0x0,1024); ReadLineFromFile(pf,ReadLine,1024);//从文件中读取一行 //去掉首尾空格 Trim(ReadLine); if ( strlen(ReadLine) < 1 ) { continue; } if ( ReadLine[0] == '[' && ReadLine[strlen(ReadLine)-1] == ']' ) { strcpy(CurrentAppName,ReadLine); } else { char tmpLineKey[1024]; char tmpLineValue[1024]; SplitKeyAndValue(ReadLine,tmpLineKey,1024,tmpLineValue,1024); if ( strcmp(CurrentAppName,RightAppName) == 0 && strcmp(tmpLineKey,KeyName) == 0 ) { strncpy(OutStr,tmpLineValue,OutStrSize); fclose(pf); return; } } } fclose(pf);}int main(void){ char AppName[1024]; printf("输入结点名称 = "); scanf("%s",AppName); char KeyName[1024]; printf("输入键名称 = "); scanf("%s",KeyName); char Value[1024]; memset(Value,0x0,1024); ReadConfigString(AppName,KeyName,"没有此配置",Value,1024,"config.ini"); printf("找到配置值为 = %s\n",Value); fflush(stdin); getchar(); return 0;}