C#读写ini文件
主要思路是调用Win32 API。
1.引入命名空间
1.引入命名空间
using System.Runtime.InteropServices;
2.声明(把一个Win32 API函数转成C#函数) //声明INI文件的写操作函数 WritePrivateProfileString()
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//声明INI文件的读操作函数 GetPrivateProfileString()
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
3.调用[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//声明INI文件的读操作函数 GetPrivateProfileString()
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
//写一个config.ini文件
private void Save_Ini()
{
string s = System.Windows.Forms.Application.ExecutablePath;
//在当前目录下,写一个config.ini文件
string path = s.ToLower().Replace("mediaconvert.exe", "config.ini");
string configureNode = "DataBaseConfigure";//配置节
string key1 = "DataBase";//键名
string key1_Value = "DataBaseName";//键值
string key2 = "Server";
string key2_Value = "ServerName";
string key3 = "UserId";
string key3_Value = "1";
WritePrivateProfileString(configureNode, key1, key1_Value, path);
WritePrivateProfileString(configureNode, key2, key2_Value, path);
WritePrivateProfileString(configureNode, key3, key3_Value, path);
/*最后在exe文件的同目录下,生成一个config.ini文件,内容应如下:
* [DataBaseConfigure]
* DataBase=DataBaseName
* Server=ServerName
* UserId=1
*/
}
private void Save_Ini()
{
string s = System.Windows.Forms.Application.ExecutablePath;
//在当前目录下,写一个config.ini文件
string path = s.ToLower().Replace("mediaconvert.exe", "config.ini");
string configureNode = "DataBaseConfigure";//配置节
string key1 = "DataBase";//键名
string key1_Value = "DataBaseName";//键值
string key2 = "Server";
string key2_Value = "ServerName";
string key3 = "UserId";
string key3_Value = "1";
WritePrivateProfileString(configureNode, key1, key1_Value, path);
WritePrivateProfileString(configureNode, key2, key2_Value, path);
WritePrivateProfileString(configureNode, key3, key3_Value, path);
/*最后在exe文件的同目录下,生成一个config.ini文件,内容应如下:
* [DataBaseConfigure]
* DataBase=DataBaseName
* Server=ServerName
* UserId=1
*/
}
//读取config.ini文件中的配置
private void Read_Ini()
{
string s = System.Windows.Forms.Application.ExecutablePath;
//取得config.ini路径
string path = s.ToLower().Replace("mediaconvert.exe", "config.ini");
StringBuilder str = new StringBuilder(255);
//取得配置节[DataBaseConfigure]的DataBase键的值
GetPrivateProfileString("DataBaseConfigure", "DataBase", "", str, 255, path);
//对话框中结果应该为 DataBase:DataBaseName
System.Windows.Forms.MessageBox.Show("DataBase:" + str.ToString());
}
private void Read_Ini()
{
string s = System.Windows.Forms.Application.ExecutablePath;
//取得config.ini路径
string path = s.ToLower().Replace("mediaconvert.exe", "config.ini");
StringBuilder str = new StringBuilder(255);
//取得配置节[DataBaseConfigure]的DataBase键的值
GetPrivateProfileString("DataBaseConfigure", "DataBase", "", str, 255, path);
//对话框中结果应该为 DataBase:DataBaseName
System.Windows.Forms.MessageBox.Show("DataBase:" + str.ToString());
}
C#使用系统Api,最头疼的问题就是Api中的数据类型在C#中,如何对应的问题。
这个网站http://www.pinvoke.net列出了大多系统Api在此C#或VB.Net中的对应声明,很详细。