WritePrivateProfileString
求助编辑百科名片
编辑本段函数在VB中的使用
所有版本通用:WritePrivateProfileString(lpApplicationName, lpKeyName, lpString, lpFileName)
VB声明
| |
Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String)
| |
说明
| |
在初始化文件指定小节内设置一个字串
| |
返回值
| |
Long,非零表示成功,零表示失败。会设置GetLastError
| |
参数表
| |
参数
|
类型及说明
|
lpApplicationName
|
String,要在其中写入新字串的小节名称。这个字串不区分大小写
|
lpKeyName
|
Any,要设置的项名或条目名。这个字串不区分大小写。用vbNullString可删除这个小节的所有设置项
|
lpString
|
String,指定为这个项写入的字串值。用vbNullString表示删除这个项现有的字串
|
lpFileName
|
String,初始化文件的名字。如果没有指定完整路径名,则windows会在windows目录查找文件。如果文件没有找到,则函数会创建它
|
相关函数:GetPrivateProfileString
编辑本段函数在VC中的使用
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:
一.将信息写入.INI文件中
1.所用的WINAPI函数原型为:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);
其中各参数的意义
LPCTSTR lpAppName 是INI文件中的一个
字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是
变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中.
CString strName,strTemp;
int nAge;
strName="
张三";
nAge=12;
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
此时c:\stud\student.ini文件中的内容如下:
[StudentInfo]
Name=
张三
3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:
strTemp.format("%d",nAge);
::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini");
二.将信息从INI文件中读入程序中的
变量
1.所用的WINAPI函数原型为:
DWORD
GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各参数的意义
前二个参数与 WritePrivateProfileString中的意义一样.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
注意:如果在读入的ini文件不存在,则按默认值生成相应的ini文件
3.读入
整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
这里的参数意义与上相同.使用方法如下:
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");
三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:
1.写入:
CString strTemp,strTempA;
int i;
int nCount=6;
文件://共有6个文件名需要保存
for(i=0;i {strTemp.format("%d",i);
strTempA=文件名;
文件://文件名可以从
数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
"c:\\usefile\\usefile.ini");
}
strTemp.format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
文件://将文件总数写入,以便读出.
2.读出:
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");
for(i=0;i {strTemp.format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");
文件://使用strTempA中的内容.
}
配置文件中经常用到ini文件,在VC中其函数分别为:
写入.ini文件:
- BOOL WritePrivateProfileString(
- LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
- LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
- LPCTSTR lpString, // 键值,也就是数据
- LPCTSTR lpFileName // INI文件的路径
- );
读取.ini文件:
- DWORD GetPrivateProfileString(
- LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
- LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
- LPCTSTR lpDefault, // 如果lpReturnedString为空,则把个变量赋给lpReturnedString
- LPTSTR lpReturnedString, // 存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区
- DWORD nSize, // lpReturnedString的缓冲区大小
- LPCTSTR lpFileName // INI文件的路径
- );
读取整形值:(返回值为读到的整)
- UINT GetPrivateProfileInt(
- LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
- LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
- INT nDefault, // 如果没有找到指定的数据返回,则把个变量值赋给返回值
- LPCTSTR lpFileName // INI文件的路径
- );
读写INI文件时相对路径和绝对路径都可以,根据实际情况选择
"..\\IniFileName.ini" // 这样的为相对路径
"D:\\IniFileName.ini" // 这样的为绝对路径
MAX_PATH:是微软最大路径占的字节所设的宏
例子:
写INI文件:
- LPTSTR lpPath = new char[MAX_PATH];
- strcpy(lpPath, "D:\\IniFileName.ini");
- WritePrivateProfileString("LiMing", "Sex", "Man", lpPath);
- WritePrivateProfileString("LiMing", "Age", "20", lpPath);
- WritePrivateProfileString("Fangfang", "Sex", "Woman", lpPath);
- WritePrivateProfileString("Fangfang", "Age", "21", lpPath);
- delete [] lpPath;
- INI文件如下:
- [LiMing]
- Sex=Man
- Age=20
- [Fangfang]
- Sex=Woman
- Age=21
- 读INI文件:
- LPTSTR lpPath = new char[MAX_PATH];
- LPTSTR LiMingSex = new char[6];
- int LiMingAge;
- LPTSTR FangfangSex = new char[6];
- int FangfangAge;
- strcpy(lpPath, "..\\IniFileName.ini");
- GetPrivateProfileString("LiMing", "Sex", "", LiMingSex, 6, lpPath);
- LiMingAge = GetPrivateProfileInt("LiMing", "Age", 0, lpPath);
- GetPrivateProfileString("Fangfang", "Sex", "", FangfangSex, 6, lpPath);
- FangfangAge = GetPrivateProfileInt("Fangfang", "Age", 0, lpPath);
- delete [] lpPath;