目录
C# 读写编辑INI文件
INI文件概念
INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打开,主要存放的是用户所做的选择或系统的各种参数。
C#读写ini文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键字(KEYWORD)和一个等号,等号右边就是关键字的值(VALUE),例如,
[Section1]
KeyWord1 = Value1 KeyWord2 = Value2 ...
[Section2]
KeyWord3 = Value3 KeyWord4 = Value4
INI文件读操作
C#命名空间中没有直接读写INI的类,虽然C#中没有,但是在"kernel32.dll"这个文件中有Win32的API函数–WritePrivateProfileString()和GetPrivateProfileString()
类文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace Files { class FilesINI { // 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); /// 写入INI的方法 public void INIWrite(string section, string key, string value,string path) { // section=配置节点名称,key=键名,value=返回键值,path=路径 WritePrivateProfileString(section, key, value, path); } //读取INI的方法 public string INIRead(string section, string key,string path) { // 每次从ini中读取多少字节 System.Text.StringBuilder temp = new System.Text.StringBuilder(255); // section=配置节点名称,key=键名,temp=上面,path=路径 GetPrivateProfileString(section, key, "", temp, 255, path); return temp.ToString(); } //删除一个INI文件 public void INIDelete(string FilePath) { File.Delete(FilePath); } } }
主函数
using Files; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace INI文件读写操作 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string IP; int Port; int Speed; //声明默认配置文件路径 public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "Config.ini"; private void Form1_Load(object sender, EventArgs e) { FilesINI ConfigINI = new FilesINI(); IP = ConfigINI.INIRead("Fanuc机器人控制参数", "IP", INIPath); Port = Convert.ToInt32(ConfigINI.INIRead("Fanuc机器人控制参数", "Port", INIPath)); Speed = Convert.ToInt32(ConfigINI.INIRead("Fanuc机器人控制参数", "Speed", INIPath)); textBox1.Text = IP; textBox2.Text = Port.ToString(); textBox3.Text = Speed.ToString(); } } }
INI文件修改操作
修改IP
private void button1_Click(object sender, EventArgs e) { FilesINI ConfigINI = new FilesINI(); ConfigINI.INIWrite("Fanuc机器人控制参数", "IP", textBox1.Text, INIPath); }
INI文件写操作
添加新的配置节点【KUKA机器人控制参数】
private void button1_Click(object sender, EventArgs e) { FilesINI ConfigINI = new FilesINI(); ConfigINI.INIWrite("KUKA机器人控制参数", "IP", textBox1.Text, INIPath); }
在某个配置节点下,添加新的关键字Angle
private void button1_Click(object sender, EventArgs e) { FilesINI ConfigINI = new FilesINI(); ConfigINI.INIWrite("KUKA机器人控制参数", "Angle", textBox1.Text, INIPath); }
C#读写INI文件案例
提示:以下是本篇文章正文内容,下面案例可供参考
一、命名空间
首先需要引用命名空间命名空间
using System.Runtime.InteropServices;
二、函数封装
代码如下(示例):
[DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath); #region 创建文件 public static void CreateFile(string path) { if (!string.IsNullOrEmpty(path)) { try { string dr = Path.GetDirectoryName(path); if (!Directory.Exists(dr)) { Directory.CreateDirectory(dr); } if (!File.Exists(path)) { FileStream fs = File.Create(path); fs.Close(); } } catch (Exception e) { } } } #endregion #region 写ini文件 ///<param name="Selection">ini文件中的节名</param> ///<param name="key">ini 文件中的健</param> ///<param name="value">要写入该健所对应的值</param> ///<param name="iniFilePath">ini文件路径</param> public static bool WriteIniData(string Section, string key, string val, string inifilePath) { if (File.Exists(inifilePath)) { long opSt = WritePrivateProfileString(Section, key, val, inifilePath); if (opSt == 0) { return false; } else { return true; } } else { CreateFile(inifilePath); long opSt = WritePrivateProfileString(Section, key, val, inifilePath); if (opSt == 0) { return false; } else { return true; } } } #endregion #region 取ini文件 /// <param name="section">节点名称</param> /// <param name="key">对应的key</param> /// <param name="noText">读不到值时返回的默认值</param> /// <param name="iniFilePath">文件路径</param> public static string ReadIniData(string section, string key, string noText, string iniFilePath) { if (File.Exists(iniFilePath)) { StringBuilder temp = new StringBuilder(1024); long k = GetPrivateProfileString(section, key, noText, temp, 1024, iniFilePath); if (k != 0) { return temp.ToString(); } else { return string.Empty; } } else { return string.Empty; } } #endregion
三、数据读写
代码如下(示例):
#region 把key——value写入ini文件 public bool savePwdToIni(string pwd) { string path = System.AppDomain.CurrentDomain.BaseDirectory; path += "\\" + "ini" + "\\Password.ini"; bool b = WriteIniData("Section_1","pwd",pwd,path); return b; } #endregion #region 从路径下的ini文件读取key对应的value public string readPwdFromIni() { string path = System.AppDomain.CurrentDomain.BaseDirectory; path += "\\" + "ini" + "\\Password.ini"; string s = ReadIniData("Section_1","pwd","error",path); return s; } //数据读写 bool b = WriteIniData("Section_1", "key", value, path); string s = ReadIniData("Section_2", "key", "error", path);
以上内容简单介绍了C#里面ini文件的读写,封装后的函数可以大幅度地提高编程效率。