在本文中,我们将带你了解使用Delphi内置函数读写INI文件在这篇文章中,我们将为您详细介绍使用Delphi内置函数读写INI文件的方方面面,并解答delphiini文件读写常见的疑惑,同时我们还将
在本文中,我们将带你了解使用Delphi内置函数读写INI文件在这篇文章中,我们将为您详细介绍使用Delphi内置函数读写INI文件的方方面面,并解答delphi ini文件读写常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的API读写INI文件(转载)、C#实现读写ini文件类实例、C#简单读写ini文件、C#读写INI文件。
本文目录一览:使用Delphi内置函数读写INI文件(delphi ini文件读写)
总结
以上是小编为你收集整理的使用Delphi内置函数读写INI文件全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
API读写INI文件(转载)
API读写INI文件(转载)
用API写INI文件的函数有
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // 节名
LPCTSTR lpKeyName, // 键名
LPCTSTR lpString, // 添加的字符串
LPCTSTR lpFileName // Ini文件名
);
BOOL WritePrivateProfileStruct(
LPCTSTR lpszSection, // pointer to section name
LPCTSTR lpszKey, // pointer to key name
LPVOID lpStruct, // 要写入的数据缓冲区
UINT uSizeStruct, // 缓冲区的大小
LPCTSTR szFile // pointer to initialization filename
);
BOOL WritePrivateProfileSection(
LPCTSTR lpAppName, // pointer to string with section name
LPCTSTR lpString, // 写入的字符串
LPCTSTR lpFileName // pointer to string with filename
);
用API读INI文件的函数有
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // points to section name
LPCTSTR lpKeyName, // points to key name
LPCTSTR lpDefault, // 默认字符串 ,如果没有则返回该值
LPTSTR lpReturnedString, // 返回的字符串
DWORD nSize, // 返回字符串的大小
LPCTSTR lpFileName // points to initialization filename
);
DWORD GetPrivateProfileSection(
LPCTSTR lpAppName, // address of section name
LPTSTR lpReturnedString, // address of return buffer
DWORD nSize, // size of return buffer
LPCTSTR lpFileName // address of initialization filename
);
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // address of section name
LPCTSTR lpKeyName, // address of key name
INT nDefault, // return value if key name is not found
LPCTSTR lpFileName // address of initialization filename
);
BOOL GetPrivateProfileStruct(
LPCTSTR lpszSection, // address of section name
LPCTSTR lpszKey, // address of key name
LPVOID lpStruct, // address of return buffer
UINT uSizeStruct, // size of return buffer
LPCTSTR szFile // address of initialization filename
);
DWORD GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // address of return buffer
DWORD nSize, // size of return buffer
LPCTSTR lpFileName // address of initialization filename
);
当然还有如WriteProfileString,WriteProfileSection,WriteProfileSturct, GetProfileString,GetProfileStruct,GetProfileSectionNames,GetProfileInt,GetProfileSection但这些只对Win.ini有效
下面我们来学习它们的用法
WritePrivateProfileString函数是向ini文件中写入字符串,如
WritePrivateProfileString(Pchar(''类型''),Pchar(''API''),Pchar(''API 真好!''),Pchar(''c:/example.ini''));
如果第二个参数是nil,那么该操作将删除该节
如果第三个参数为nil,那么该操作将删除该节中的所有键
如果在指定的文件中没有路径,那么它将在系统的目录寻找文件,如果不存在则建立
WritePrivateProfileSection是向文件中写入一整个键,其它键的格式为key = value,如
WritePrivateProfileSection(Pchar(''类型''),Pchar(''其它=123''),Pchar(''c:/example.ini''));
注意,该操作将删除该节中的所有键后在进行本次的写入
WritePrivateProfileStruct是向文件中写入一个结构,如
type
TPerson = record
Name:string;
Age:integer;
end;
var
Per:TPerson;
WritePrivateProfileStruct(Pchar(''类型''),Pchar(''结构''),@Per ,Sizeof(Per),Pchar(''C:/example.ini''));
GetPrivateProfileString是从文件中读出一个指定键的字符串,如果没有找到,则返回默认值
GetPrivateProfileString(Pchar(‘类型''),Pchar(''API''),Pchar(''no value''),Str1,21,Pchar(''c:/example.ini''));
GetPrivateProfileSection是从文件中读出一整个键
GetprivateProfileSection(''类型'',str1,200,''c:/example.ini'');
GetPrivateProfileInt是从文件中读出指定键的整型值,如果没找到或不是整型的值,则返回默认值,如果返回值小于0,则返回0,如
i:=GetPrivateProfileInt(Pchar(''类型''),Pchar(''整型''),i,Pchar(''C:/example.ini''));
showMessage(inttostr(i));
GetPrivateProfileStruct从文件中读出指定键的结构值,如
type
TPerson = record
Name:string;
Age:integer;
end;
var
Buffer:TPerson;
GetPrivateProfileStruct(Pchar(''类型''),Pchar(''结构''),@Buffer ,Sizeof(Per),Pchar(''C:/example.ini''));
GetPrivateProfileSectionNames是从文件中读出所有节的名称,该函数返回读入缓冲区的字符数,如
count:=GetPrivateProfileSectionNames(Str3,200,Pchar(''c:/example.ini''));
ShowMessage(Str3);
此处显示的只是第一个节的名称,如果要显示第二个字符的名称则要用到下面这句
showmessage(str3+5);
这句不用多解释吧?
以上就是这些函数的用法。你可能要问“怎么只有写字符串的呀,怎么没有写其它的类型的呢?”,问的好,不过其它类型的用WritePrivateProfileString都能代替,如要写浮点型的就把该类型的放到’’当中,如’12.5’。那位学友又问了,“如果是让用户输入,他们也不知道应该输入什么,怎么能限制他们输入的都是数字”,这方法可太多了,如用控件或检查它们是不是在指定的数字,你可别说不会限制它们是不是数字呀*_^,如果真的不会,你就看它们的ASCII码是不是在48-57之间就行了。“那读出呢?”,Delphi不是有StrToInt,StrToFloat,StrToCurr等这么函数嘛,可以用它们来转换。
我在研究这些函数的同时,发现了一段有趣程序代码,但我不知道它为什么要这样做,有什么好处,大家可以看看它们,不过是用C写的,它们在Delphi SDK或MSDN中查找WritePrivateProfileString函数,在最下面的那个段代码就是
如果我在上面说的有错误的话,希望大家指正,共同讨论学习
下面是另一网友文章
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
VC中用函数读写ini文件的方法
ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息。ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Key可以赋相应的值。读写ini文件实际上就是读写某个的Section中相应的Key的值,而这只要借助几个函数即可完成。
一、向ini文件中写入信息的函数
1. 把信息写入系统的win.ini文件
BOOL WriteProfileString(
LPCTSTR lpAppName, // 节的名字,是一个以0结束的字符串
LPCTSTR lpKeyName, // 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
LPCTSTR lpString // 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
)
2. 把信息写入自己定义的.ini文件
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
LPCTSTR lpString, // 同上
LPCTSTR lpFileName // 要写入的文件的文件名。若该ini文件与程序在同一个目录下,也可使用相对
//路径,否则需要给出绝度路径。
)
如:
::WriteProfileString("Test","id","xym");
//在win.ini中创建一个Test节,并在该节中创建一个键id,其值为xym
::WritePrivateProfileString("Test","id","xym","d://vc//Ex1//ex1.ini");
//在Ex1目录下的ex1.ini中创建一个Test节,并在该节中创建一个键id,其值为xym
//若Ex1.ini文件与读写该文件的程序在同一个目录下,则上面语句也可写为:
::WritePrivateProfileString("Test","id","xym",".//ex1.ini");
需要注意的是,C系列的语言中,转义字符''//''表示反斜线''/''。另外,当使用相对路径时,//前的.号不能丢掉了。
二、从ini文件中读取数据的函数
1、从系统的win.ini文件中读取信息
(1) 读取字符串
DWORD GetProfileString(
LPCTSTR lpAppName, // 节名
LPCTSTR lpKeyName, // 键名,读取该键的值
LPCTSTR lpDefault, // 若指定的键不存在,该值作为读取的默认值
LPTSTR lpReturnedString, // 一个指向缓冲区的指针,接收读取的字符串
DWORD nSize // 指定lpReturnedString指向的缓冲区的大小
)
如:
CString str;
::GetProfileString("Test","id","Error",str.GetBuffer(20),20);
(2) 读取整数
UINT GetProfileInt(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault // 若指定的键名不存在,该值作为读取的默认值
)
如使用以下语句写入了年龄信息:
::WriteProfileString("Test","age","25");
//在win.ini中创建一个Test节,并在该节中创建一个键age,其值为25
则可用以下语句读取age键的值:
int age;
age=::GetProfileInt("Test","age",0);
2、从自己的ini文件中读取信息
(1) 读取字符串
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // 同1(1)
LPCTSTR lpKeyName, // 同1(1)
LPCTSTR lpDefault, // 同1(1)
LPTSTR lpReturnedString, // 同1(1)
DWORD nSize, // 同1(1)
LPCTSTR lpFileName // 读取信息的文件名。若该ini文件与程序在同一个目录下,也可使用相
//对路径,否则需要给出绝度路径。
)
如:
CString str;
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,".//ex1.ini");
或:
::GetPrivateProfileString("Test","id","Error",str.GetBuffer(20),20,"d://vc//Ex1//ex1.ini");
(2) 读取整数
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault, // 若指定的键名不存在,该值作为读取的默认值
LPCTSTR lpFileName // 同上
)
如使用以下语句写入了年龄信息:
::WritePrivateProfileString("Test","age","25",".//ex1.ini");
//在ex1.ini中创建一个Test节,并在该节中创建一个键age,其值为25
则可用以下语句读取age键的值:
int age;
age=::GetPrivateProfileInt("Test","age",0,".//ex1.ini");
三、 删除键值或节
回顾一下WriteProfileString函数的说明
BOOL WriteProfileString(
LPCTSTR lpAppName, // 节的名字,是一个以0结束的字符串
LPCTSTR lpKeyName, // 键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
LPCTSTR lpString // 键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
)
由此可见,要删除某个节,只需要将WriteProfileString第二个参数设为NULL即可。而要删除某个键,则只需要将该函数的第三个参数设为 NULL即可。这是删除系统的win.ini中的节或键,类似的,要删除自己定义的ini文件中的节或键,也可做相同的操作。
如:
::WriteProfileString("Test",NULL,NULL); //删除win.ini中的Test节
::WriteProfileString("Test","id",NULL); //删除win.ini中的id键
::WritePrivateProfileString("Test",NULL,NULL,".//ex1.ini"); //删除ex1.ini中的Test节
::WritePrivateProfileString("Test","id",NULL,".//ex1.ini"); //删除ex1.ini中的id键
四、如何判断一个ini文件中有多少个节
要判断一个ini文件中有多少个节,最简单的办法就是将所有的节名都找出来,然后统计节名的个数。而要将所有的节名找出来,使用GetPrivateProfileSectionNames函数就可以了,其原型如下:
DWORD GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // 指向一个缓冲区,用来保存返回的所有节名
DWORD nSize, // 参数lpszReturnBuffer的大小
LPCTSTR lpFileName // 文件名,若该ini文件与程序在同一个目录下,
//也可使用相对路径,否则需要给出绝度路径
)
下面的是用来统计一个ini文件中共有多少个节的函数,当然,如果需要同时找到每个节中的各个键及其值,根据找到节名就可以很容易的得到了。
/*统计共有多少个节
节名的分离方法:若chSectionNames数组的第一字符是''/0''字符,则表明
有0个节。否则,从chSectionNames数组的第一个字符开始,顺序往后找,
直到找到一个''/0''字符,若该字符的后继字符不是 ''/0''字符,则表明前
面的字符组成一个节名。若连续找到两个''/0''字符,则统计结束*/
int CTestDlg::CalcCount(void)
{
TCHAR chSectionNames[2048]={0}; //所有节名组成的字符数组
char * pSectionName; //保存找到的某个节名字符串的首地址
int i; //i指向数组chSectionNames的某个位置,从0开始,顺序后移
int j=0; //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量
int count=0; //统计节的个数
//CString name;
//char id[20];
::GetPrivateProfileSectionNames(chSectionNames,2048,".//ex1.ini");
for(i=0;i<2048;i++,j++)
{
if(chSectionNames[0]==''/0'')
break; //如果第一个字符就是0,则说明ini中一个节也没有
if(chSectionNames[i]==''/0'')
{
pSectionName=&chSectionNames[i-j]; //找到一个0,则说明从这个字符往前,减掉j个偏移量,
//就是一个节名的首地址
j=-1; //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量
//赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名
//的一部分
/*::GetPrivateProfileString(pSectionName,"id","Error",id,20,".//ex1.ini");
name.Format("%s",id);*/
//在获取节名的时候可以获取该节中键的值,前提是我们知道该节中有哪些键。
AfxMessageBox(pSectionName); //把找到的显示出来
if(chSectionNames[i+1]==0)
{
break; //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止
}
}
}
return count;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
在VC程序中利用系统提供的GetPrivateProfileString及WritePrivateProfileString函数直接读写系统配置ini文件(指定目录下的Ini文件)
假设在当前目录下有一个文件名为Tets.ini的文件
用于保存用户名和密码
文件格式如下:
[Section1]
Item1=huzhifeng
Item2=1234565
1.写INI文件
void CINI_File_TestDlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here
CString strSection = "Section1";
CString strSectionKey = "Item1";
char strBuff[256];
CString strValue = _T("");
CString strFilePath;
strFilePath=GetCurrentDirectory(256,strBuff); //获取当前路径
strFilePath.Format("%s//Test.ini",strBuff);
GetDlgItemText(IDC_EDIT_NAME,strValue); //获取文本框内容:即姓名
WritePrivateProfileString(strSection,strSectionKey,strValue,strFilePath); //写入ini文件中相应字段
strSectionKey="Item2";
GetDlgItemText(IDC_EDIT_PASSWORD,strValue); //获取文本框内容:即密码
WritePrivateProfileString(strSection,strSectionKey,strValue,strFilePath);
}
2.读INI文件内容
void CINI_File_TestDlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
CString strSection = "Section1";
CString strSectionKey = "Item1";
char strBuff[256];
CString strValue = _T("");
CString strFilePath;
strFilePath=GetCurrentDirectory(256,strBuff); //获取当前路径
strFilePath.Format("%s//Test.ini",strBuff);
GetPrivateProfileString(strSection,strSectionKey,NULL,strBuff,80,strFilePath); //读取ini文件中相应字段的内容
strValue=strBuff;
SetDlgItemText(IDC_EDIT_NAME,strValue);
strSectionKey="Item2";
GetPrivateProfileString(strSection,strSectionKey,NULL,strBuff,80,strFilePath);
strValue=strBuff;
SetDlgItemText(IDC_EDIT_PASSWORD,strValue);
UpdateData(FALSE);
}
C#实现读写ini文件类实例
本文实例讲述了C#实现读写ini文件类。分享给大家供大家参考。具体如下:
这个C#类封装了对INI配置文件进行操作所需的各种函数,包括读取键值、读取键值、删除段落等
using System; using System.Runtime.InteropServices; using System.Text; namespace DotNet.Utilities { /// <summary> /// INI文件读写类。 /// </summary> public class INIFile { public string path; public INIFile(string INIPath) { path = INIPath; } [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,string key,string val,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,string def,StringBuilder retVal,int size,string defVal,Byte[] retVal,string filePath); /// <summary> /// 写INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <param name="Value"></param> public void IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); } /// <summary> /// 读取INI文件 /// </summary> /// <param name="Section"></param> /// <param name="Key"></param> /// <returns></returns> public string IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,"",temp,255,this.path); return temp.ToString(); } public byte[] IniReadValues(string section,string key) { byte[] temp = new byte[255]; int i = GetPrivateProfileString(section,key,this.path); return temp; } /// <summary> /// 删除ini文件下所有段落 /// </summary> public void ClearallSection() { IniWriteValue(null,null,null); } /// <summary> /// 删除ini文件下personal段落下的所有键 /// </summary> /// <param name="Section"></param> public void ClearSection(string Section) { IniWriteValue(Section,null); } } }
希望本文所述对大家的C#程序设计有所帮助。
C#简单读写ini文件
INI文件其实是一种文本文件,它的构成分为三部分 Section Key Value
[section1]
key1=value11
key2=value12
[section2]
key1=value21
key2=value22
Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePrivateProfileString()分别实现了对INI文件的读写操作 所以可以写一个简单的类来读写ini文件,以满足一般应用的配置存取需求:
class IniFile
{
/*
* 声明API函数
*/
public string iniPath;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// 构造函数
/// </summary>
/// <param name="iniPath">ini文件路径,默认为当前路径下default.ini</param>
public IniFile(string iniPath = "./default.ini")
{
this.iniPath = iniPath;
}
/// <summary>
/// 写入ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void writeIni(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.iniPath);
}
/// <summary>
/// 写入ini文件,不管section,默认放在default里
/// </summary>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void writeIni(string Key, string Value)
{
WritePrivateProfileString("default", Key, Value, this.iniPath);
}
/// <summary>
/// 读取ini文件
/// </summary>
/// <param name="Section">Section</param>
/// <param name="Key">键</param>
/// <returns>返回的值</returns>
public string readIni(string Section, string Key)
{
StringBuilder temp = new StringBuilder(256);
int i = GetPrivateProfileString(Section, Key, "", temp, 256, this.iniPath);
return temp.ToString();
}
/// <summary>
/// 读取section,不管section,默认从default里读取
/// </summary>
/// <param name="Key">键</param>
/// <returns>返回值</returns>
public string readIni(string Key)
{
return readIni("default", Key);
}
/// <summary>
/// 查询ini文件是否存在
/// </summary>
/// <returns>是否存在</returns>
public bool existINIFile()
{
return File.Exists(iniPath);
}
}
调用方式:
class Program
{
static void Main(string[] args)
{
IniFile iniFile = new IniFile("./hello.ini");
iniFile.writeIni("section1", "key1", "value11");
iniFile.writeIni("section1", "key2", "value12");
iniFile.writeIni("section2", "key1", "value21");
iniFile.writeIni("section2", "key2", "value22");
iniFile.writeIni("key", "value");
string str = iniFile.readIni("key");
Console.WriteLine(str);
Console.ReadKey();
}
}
C#读写INI文件
虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。
INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value):
[Section] Key=Value
VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。
INIFILE类:
using System; using System.IO; using System.Runtime.InteropServices;
因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。
using System.Text; namespace Ini { public class IniFile { public string path; //INI文件名 [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,string key, string val,string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,string key,string def, StringBuilder retVal,int size,string filePath); //声明读写INI文件的API函数 public IniFile(string INIPath) { path = INIPath; } //类的构造函数,传递INI文件名 publicvoid IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); } //写INI文件 publicstring IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); } //读取INI文件指定 } }
调用INIFILE类:
新建一个标准的C# WINDOWS应用程序项目,在窗体中分别增加命名为sect、key、val的三个文本框。
增加如下代码:
using Ini; //创建命名空间 //当窗体关闭时保存窗体坐标 privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { IniFile ini = new IniFile("C://test.ini"); ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString()); ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString()); //ToString方法将数字转换为字符串 } //当窗体启动时,读取INI文件的值并赋值给窗体 privatevoid Form1_Load(object sender, System.EventArgs e) { IniFile ini = new IniFile("C://test.ini"); Point p=new Point(); //判断返回值,避免第一次运行时为空出错 if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!="")) { p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" )); p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" )); // int.Parse将字符串转换为int this.Location =p; } }
原文链接: http://blog.csdn.net/21aspnet/article/details/160229
我们今天的关于使用Delphi内置函数读写INI文件和delphi ini文件读写的分享已经告一段落,感谢您的关注,如果您想了解更多关于API读写INI文件(转载)、C#实现读写ini文件类实例、C#简单读写ini文件、C#读写INI文件的相关信息,请在本站查询。
本文标签: