准备:
新建文本文档(.txt)修改扩展名为.ini,打开写入
[Settings]
Text=
R= 255
G= 255
B= 255
引用的API结构:
[System.Runtime.InteropServices.DllImport( " kernel32 ")]
private static extern long WritePrivateProfileString( string section, string key, string val, string filePath);
[System.Runtime.InteropServices.DllImport( " kernel32 ")]
private static extern int GetPrivateProfileString( string section, string key, string def,StringBuilder retVal, int size, string filePath);
读取方法:
private void Form1_Activated( object sender, EventArgs e)
{
textBox1.Text = GetIniValue( " Settings ", " Text ");
int R = int.Parse(GetIniValue( " Settings ", " R "));
int G = int.Parse(GetIniValue( " Settings ", " G "));
int B = int.Parse(GetIniValue( " Settings ", " B "));
textBox1.BackColor = Color.FromArgb(R, G, B);
Activated -= new EventHandler(Form1_Activated);
}
private string GetIniValue( string section, string key)
{
StringBuilder sb = new StringBuilder( 255); // 255为字符串长度
GetPrivateProfileString(section, key, "", sb, 255, path);
return sb.ToString();
}
保存方法:
private void Form1_FormClosing( object sender, FormClosingEventArgs e)
{
WritePrivateProfileString( " Settings ", " Text ", textBox1.Text, path);
WritePrivateProfileString( " Settings ", " R ", textBox1.BackColor.R.ToString(), path);
WritePrivateProfileString( " Settings ", " G ", textBox1.BackColor.G.ToString(), path);
WritePrivateProfileString( " Settings ", " B ", textBox1.BackColor.B.ToString(), path);
}
新建文本文档(.txt)修改扩展名为.ini,打开写入
[Settings]
Text=
R= 255
G= 255
B= 255
引用的API结构:
[System.Runtime.InteropServices.DllImport( " kernel32 ")]
private static extern long WritePrivateProfileString( string section, string key, string val, string filePath);
[System.Runtime.InteropServices.DllImport( " kernel32 ")]
private static extern int GetPrivateProfileString( string section, string key, string def,StringBuilder retVal, int size, string filePath);
读取方法:
private void Form1_Activated( object sender, EventArgs e)
{
textBox1.Text = GetIniValue( " Settings ", " Text ");
int R = int.Parse(GetIniValue( " Settings ", " R "));
int G = int.Parse(GetIniValue( " Settings ", " G "));
int B = int.Parse(GetIniValue( " Settings ", " B "));
textBox1.BackColor = Color.FromArgb(R, G, B);
Activated -= new EventHandler(Form1_Activated);
}
private string GetIniValue( string section, string key)
{
StringBuilder sb = new StringBuilder( 255); // 255为字符串长度
GetPrivateProfileString(section, key, "", sb, 255, path);
return sb.ToString();
}
保存方法:
private void Form1_FormClosing( object sender, FormClosingEventArgs e)
{
WritePrivateProfileString( " Settings ", " Text ", textBox1.Text, path);
WritePrivateProfileString( " Settings ", " R ", textBox1.BackColor.R.ToString(), path);
WritePrivateProfileString( " Settings ", " G ", textBox1.BackColor.G.ToString(), path);
WritePrivateProfileString( " Settings ", " B ", textBox1.BackColor.B.ToString(), path);
}