C#读写.ini文件
2015-08-03 14:31
423 查看
C#代码中要读写.ini文件,需要导入kernel32.dll中的GetPrivateProfileString和WritePrivateProfileString两个函数。
首先,介绍一下kernel32.dll。
kernel32.dll是Windows9x/Me中非常重要的32位动态链接库文件,属于内核级文件。
它控制着系统的内存管理、数据的输入输出操作和中断处理。
当Windows启动时,kernel32.dll就驻留在内存中特定的写保护区域,使别的程序无法占用这个内存区域。
接下来看一下GetPrivateProfileString和WritePrivateProfileString两个函数的原型。
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
);
参数说明:
lpAppName
[in] 指针指向一个空终止字符串,指定包含密钥名节的名称。如果该参数为NULL,GetPrivateProfileString功能将文件复制到提供的缓冲区中的所有部分的名称。
lpKeyName
[in] 指向空终止字符串,指定其关联的字符串键的名称将被检索。如果这个参数为NULL,由AppName的参数指定的部分的所有键名被复制到由返回的字符串参数指定的缓冲区。
lpDefault
[in] 指针指向一个空结束的默认字符串。如果lpKeyName密钥不能在初始化文件中找到,GetPrivateProfileString副本的缺省字符串到lpReturnedString缓冲器。此参数不能为NULL。
lpReturnedString
[out] 指向接收所检索的字符串的缓冲区。
nSize
[in] 指定的大小,以返回tchars,缓冲所指向lpReturnedString参数。
lpFileName
[in] 指针指向一个空终止字符串,指定初始化文件的名称。如果该参数不包含文件的完整路径,系统搜索在Windows目录中的文件。
返回值:
返回值是复制到缓冲区,不包括终止空字符的字符数。
***************************************************************************************************************************************
CSDN中对几个类型的解释:
DWORD: A 32-bit unsigned integer or the address of a segment and its associated offset.
LPCTSTR:A 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
LPTSTR :A 32-bit pointer to a character string that is portable for Unicode and DBCS.
LPCSTR: A 32-bit pointer to a constant character string.
LPSTR : A 32-bit pointer to a character string.
其中:
L: 表示long指针。
P: 表示这是一个指针。
T: 表示_T宏,这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么这个字符或者字符串将被作为UNICODE字符串,否则就是标准的ANSI字符串。
STR:表示这个变量是一个字符串。
C: 表示是一个常量,const。
***************************************************************************************************************************************
C#导入DLL类型对应关系如下:
c++:WORD ---- c#:ushort
c++:DWORD ---- c#:uint
c++:DWORD ---- c#:int
c++:LPCTSTR ---- c#:StringBuilder
c++:LPCTSTR ---- c#:string
c++:LPCSTR(const char*) ---- c#:System.String
c++:LPTSTR 输出变量名 ---- c#:StringBuilder 输出变量名
所以在C#导入GetPrivateProfileString函数时,由int代替DWORD;由string代替LPCTSTR;由StringBuilder输出变量代替LPTSTR输出变量。
得出:
DWORD GetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName,LPCTSTR lpDefault, LPTSTR lpReturnedString,DWORD nSize,LPCTSTR lpFileName);
变为:
public static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
*******************************************************************************************************************************************
最终使用:
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
public int ReadIni()
{
string str = "D:\\123.ini";
StringBuilder temp = new StringBuilder();
GetPrivateProfileString("ScreenOrder", "LoginWindow", "1", temp, 255, str);
string str1 = temp.ToString();
int i = int.Parse(str1);
return i;
}
*******************************************************************************************************************************************
以上阐述了C#如何读取.ini文件,对于C#写入配置文件则非常相似,便不再赘述。
原型:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpString, // string to add
LPCTSTR lpFileName // initialization file
);
根据以上阐述的C#导入C++ DLL的类型转换可以得到C#中函数如下:
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int WritePrivateProfileString(string Section, string Key, string Value, string iniFilePath);
首先,介绍一下kernel32.dll。
kernel32.dll是Windows9x/Me中非常重要的32位动态链接库文件,属于内核级文件。
它控制着系统的内存管理、数据的输入输出操作和中断处理。
当Windows启动时,kernel32.dll就驻留在内存中特定的写保护区域,使别的程序无法占用这个内存区域。
接下来看一下GetPrivateProfileString和WritePrivateProfileString两个函数的原型。
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
);
参数说明:
lpAppName
[in] 指针指向一个空终止字符串,指定包含密钥名节的名称。如果该参数为NULL,GetPrivateProfileString功能将文件复制到提供的缓冲区中的所有部分的名称。
lpKeyName
[in] 指向空终止字符串,指定其关联的字符串键的名称将被检索。如果这个参数为NULL,由AppName的参数指定的部分的所有键名被复制到由返回的字符串参数指定的缓冲区。
lpDefault
[in] 指针指向一个空结束的默认字符串。如果lpKeyName密钥不能在初始化文件中找到,GetPrivateProfileString副本的缺省字符串到lpReturnedString缓冲器。此参数不能为NULL。
lpReturnedString
[out] 指向接收所检索的字符串的缓冲区。
nSize
[in] 指定的大小,以返回tchars,缓冲所指向lpReturnedString参数。
lpFileName
[in] 指针指向一个空终止字符串,指定初始化文件的名称。如果该参数不包含文件的完整路径,系统搜索在Windows目录中的文件。
返回值:
返回值是复制到缓冲区,不包括终止空字符的字符数。
***************************************************************************************************************************************
CSDN中对几个类型的解释:
DWORD: A 32-bit unsigned integer or the address of a segment and its associated offset.
LPCTSTR:A 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
LPTSTR :A 32-bit pointer to a character string that is portable for Unicode and DBCS.
LPCSTR: A 32-bit pointer to a constant character string.
LPSTR : A 32-bit pointer to a character string.
其中:
L: 表示long指针。
P: 表示这是一个指针。
T: 表示_T宏,这个宏用来表示你的字符是否使用UNICODE, 如果你的程序定义了UNICODE或者其他相关的宏,那么这个字符或者字符串将被作为UNICODE字符串,否则就是标准的ANSI字符串。
STR:表示这个变量是一个字符串。
C: 表示是一个常量,const。
***************************************************************************************************************************************
C#导入DLL类型对应关系如下:
c++:WORD ---- c#:ushort
c++:DWORD ---- c#:uint
c++:DWORD ---- c#:int
c++:LPCTSTR ---- c#:StringBuilder
c++:LPCTSTR ---- c#:string
c++:LPCSTR(const char*) ---- c#:System.String
c++:LPTSTR 输出变量名 ---- c#:StringBuilder 输出变量名
所以在C#导入GetPrivateProfileString函数时,由int代替DWORD;由string代替LPCTSTR;由StringBuilder输出变量代替LPTSTR输出变量。
得出:
DWORD GetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName,LPCTSTR lpDefault, LPTSTR lpReturnedString,DWORD nSize,LPCTSTR lpFileName);
变为:
public static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
*******************************************************************************************************************************************
最终使用:
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
public int ReadIni()
{
string str = "D:\\123.ini";
StringBuilder temp = new StringBuilder();
GetPrivateProfileString("ScreenOrder", "LoginWindow", "1", temp, 255, str);
string str1 = temp.ToString();
int i = int.Parse(str1);
return i;
}
*******************************************************************************************************************************************
以上阐述了C#如何读取.ini文件,对于C#写入配置文件则非常相似,便不再赘述。
原型:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // section name
LPCTSTR lpKeyName, // key name
LPCTSTR lpString, // string to add
LPCTSTR lpFileName // initialization file
);
根据以上阐述的C#导入C++ DLL的类型转换可以得到C#中函数如下:
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int WritePrivateProfileString(string Section, string Key, string Value, string iniFilePath);
相关文章推荐
- C#读写.ini文件
- C# 动态生成类 枚举等
- C#计算多边形面积,判断绘制多边形的方向
- C#高级编程六十五天----表达式树
- (转)c# Linq及Lamda表达式应用经验之 GroupBy 分组
- Mono for Unreal Engine发布,C#进入虚幻引擎(Unreal Engine)
- C# I/O 助手类
- C# 序列化 只读属性
- C#创建一个polygon类
- C# 模拟from表单提交webservice
- C#匿名类的使用
- C#高级编程六十四天----并行扩展
- C#获取进程的主窗口句柄的实现方法
- C# winform键盘钩子
- C#如何自动选择出系统中最合适的IP地址
- C#将字节数组加到List中
- C#读取数组中的指定长度
- C#中的 SET ,GET
- C# 冒泡排序
- C#在获取当前方法名、类名