关于C# Winform调用系统接口操作INI配置文件的代码,下面是详细的攻略:
1. 什么是INI文件
INI文件是一种配置文件格式,全称叫做Initial file,是一种比较老式的配置文件格式。它的结构非常简单,通常包含了若干个节(section)和各个节下的键值对(key-value pair)。INI文件的格式如下:
[section1]
key1=value1
key2=value2
[section2]
key1=value1
key2=value2
INI文件格式的优点是比较简单易懂,缺点是没有像JSON或者XML这类格式那样的结构化嵌套,不够灵活。
2. C# Winform调用系统接口操作INI文件
在C# Winform中,调用系统接口来操作INI文件的方式主要有以下几种:
2.1 使用Kernel32.dll文件
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string section,
string key,
string def,
StringBuilder retVal,
int size,
string filePath);
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(
string section,
string key,
string val,
string filePath);
public string ReadIni(string sectionName, string keyName, string fileName)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(sectionName, keyName, "", temp, 1024, fileName);
return temp.ToString();
}
public bool WriteIni(string sectionName, string keyName, string keyValue, string path)
{
try
{
WritePrivateProfileString(sectionName, keyName, keyValue, path);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}
2.2 使用System.Configuration.Ini配置文件
using System.Configuration;
using System.IO;
public string ReadIni(string section, string key, string defValue, string filePath)
{
var fileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = filePath
};
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string value = config.AppSettings.Settings[section + "." + key].Value;
return value ?? defValue;
}
public void WriteIni(string section, string key, string value, string filePath)
{
var fileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = filePath
};
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
config.AppSettings.Settings[section + "." + key].Value = value;
config.Save(ConfigurationSaveMode.Modified);
}
2.3 使用Ini类库
using IniParser;
using IniParser.Model;
public string ReadIni(string sectionName, string keyName, string filePath)
{
var parser = new FileIniDataParser();
IniData data = parser.ReadFile(filePath);
string value = data[sectionName][keyName];
return value;
}
public void WriteIni(string sectionName, string keyName, string keyValue, string filePath)
{
var parser = new FileIniDataParser();
IniData data = parser.ReadFile(filePath);
data[sectionName][keyName] = keyValue;
parser.WriteFile(filePath, data);
}
3. 示例使用说明
以第二种方法为例,假如我们有一个配置文件config.ini,其内容如下:
[database]
host=127.0.0.1
port=3306
database=test_db
username=root
password=password
现在我们想要在Winform应用程序中读取并显示这些配置项的值,可以这样做:
string host = ReadIni("database", "host", "127.0.0.1", "config.ini");
string port = ReadIni("database", "port", "3306", "config.ini");
string database = ReadIni("database", "database", "test_db", "config.ini");
string username = ReadIni("database", "username", "root", "config.ini");
string password = ReadIni("database", "password", "password", "config.ini");
textBoxHost.Text = host;
textBoxPort.Text = port;
textBoxDatabase.Text = database;
textBoxUsername.Text = username;
textBoxPassword.Text = password;
当然,也可以通过Winform控件来让用户自己选择配置文件的路径,并读取相应的配置项:
private void buttonBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.InitialDirectory = Application.StartupPath;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
textBoxFilePath.Text = path;
string host = ReadIni("database", "host", "127.0.0.1", path);
string port = ReadIni("database", "port", "3306", path);
string database = ReadIni("database", "database", "test_db", path);
string username = ReadIni("database", "username", "root", path);
string password = ReadIni("database", "password", "password", path);
textBoxHost.Text = host;
textBoxPort.Text = port;
textBoxDatabase.Text = database;
textBoxUsername.Text = username;
textBoxPassword.Text = password;
}
}
以上就是C# Winform调用系统接口操作INI文件的完整攻略,包含了三种方式的代码示例和具体使用方法的说明。如果还有不明白的地方,欢迎继续提问。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# Winform 调用系统接口操作 INI 配置文件的代码 - Python技术站