C#中怎么操作INI配置文件,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
创新互联网站建设公司,提供网站制作、成都网站设计,网页设计,建网站,PHP网站建设等专业做网站服务;可快速的进行网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,是专业的做网站团队,希望更多企业前来合作!源代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("kernel32.dll")] private static extern long WritePrivateProfileString(string section, string key, string value, string filepath); [DllImport("kernel32.dll")] private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder returnvalue,intbuffersize,string filepath); private string IniFilePath; private void Form1_Load(object sender, EventArgs e) { comboBox1.Text = "男"; for (int i = 1; i <= 100; i++) { comboBox2.Items.Add(i.ToString()); } comboBox2.Text = "18"; IniFilePath = Application.StartupPath + "\\Config.ini"; } private void button1_Click(object sender, EventArgs e) { if ((textBox1.Text.Trim() != "") && (textBox2.Text.Trim() != "")) { string Section = "Information"; try { WritePrivateProfileString(Section, "Name", textBox1.Text.Trim(), IniFilePath); WritePrivateProfileString(Section, "Gender", comboBox1.Text, IniFilePath); WritePrivateProfileString(Section, "Age", comboBox2.Text, IniFilePath); WritePrivateProfileString(Section, "Region", textBox2.Text.Trim(), IniFilePath); } catch (Exception ee) { MessageBox.Show(ee.Message); } } else { MessageBox.Show("姓名或地区不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void button2_Click(object sender, EventArgs e) { string outString; try { GetValue("Information", "Name", out outString); textBox1.Text = outString; GetValue("Information", "Gender", out outString); comboBox1.Text = outString; GetValue("Information", "Age", out outString); comboBox2.Text = outString; GetValue("Information", "Region", out outString); textBox2.Text = outString; } catch (Exception ee) { MessageBox.Show(ee.Message); } } private void GetValue(string section,string key, out string value) { StringBuilder stringBuilder = new StringBuilder(); GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath); value = stringBuilder.ToString(); } private void button3_Click(object sender, EventArgs e) { textBox1.Text = ""; comboBox1.Text = "男"; comboBox2.Text = "18"; textBox2.Text = ""; } } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联成都网站建设公司行业资讯频道,感谢您对创新互联成都网站建设公司的支持。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站建设公司香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。