C#对config.ini文件进行读取和修改:
public partial class Patrolcar : Form之后可以加入如下类:
#region
public class IniFile
{
public string map_length, map_width, maxnum_connect, net_ip;
public string path;
[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); public IniFile(string INIPath)
{
path = INIPath;
} public void getIni()
{
map_length = IniReadValue("setting", "map_length").ToString();
map_width = IniReadValue("setting", "map_width").ToString();
maxnum_connect = IniReadValue("setting", "maxnum_connect").ToString();
net_ip = IniReadValue("setting", "net_ip").ToString();
} public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
} public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder();
int i = GetPrivateProfileString(Section, Key, "", temp, , this.path);
return temp.ToString();
}
} IniFile dgd_ini = new IniFile(System.IO.Path.Combine(Application.StartupPath, "config.ini"));
#endregion
然后在InitializeComponent();语句之后就可以进行函数操作了:
InitializeComponent();
dgd_ini.getIni();
config.ini文件位于...\bin\Debug目录下。