C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#读写操作INI

C#对INI文件进行读写操作的示例代码

作者:lingxiao16888

这篇文章主要为大家详细介绍了C#对INI文件进行读写操作的相关知识,文中的示例代码讲解详细,具有一定的学习价值,有需要的小伙伴可以参考一下

1.通过调用API函数完成对INI文件的读取,修改,写入操作

 string filePath;
        private void btnModify_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
            {
                using (SaveFileDialog sfd=new SaveFileDialog())
                {                    
                    sfd.Filter= "初始化文件(*.INI)|*.INI";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = System.IO.File.Create(sfd.FileName);
                        fs.Dispose();
                        filePath = sfd.FileName;
                    }
                }
            }
            INIHelper.WriteToINI(filePath, "ConnectString", "Data Source", txtServer.Text);
            INIHelper.WriteToINI(filePath, "ConnectString", "DataBase", txtDataBase.Text);
            INIHelper.WriteToINI(filePath, "ConnectString", "Uid", txtSid.Text);
            INIHelper.WriteToINI(filePath, "ConnectString", "Pwd", txtPwd.Text);
            MessageBox.Show("保存成功!");
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            using(OpenFileDialog ofd=new OpenFileDialog())
            {
                ofd.Filter = "初始化文件(*.INI)|*.INI";
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                     filePath = ofd.FileName;
                    string source = INIHelper.ReadFromINI(filePath, "ConnectString", "Data Source");
                    string dataName= INIHelper.ReadFromINI(filePath, "ConnectString", "DataBase");
                    string uid= INIHelper.ReadFromINI(filePath, "ConnectString", "Uid", "未设置");
                    string pwd= INIHelper.ReadFromINI(filePath, "ConnectString", "Pwd","未设置");
                    txtServer.Text = source;
                    txtDataBase.Text = dataName;
                    txtSid.Text = uid;
                    txtPwd.Text = pwd;
                }
            }
        }
    }
    /// <summary>
    /// 对INI文件进行读写
    /// </summary>
    class INIHelper
    {
        /// <summary>
        /// 从INI文件中读取数据
        /// </summary>
        /// <param name="filePath">INI文件的全路径</param>
        /// <param name="rootValue">根节点值,例如根节点[ConnectString]的值为:ConnectString</param>
        /// <param name="key">根节点下的键</param>
        /// <param name="defValue">当标记值未设定或不存在时的默认值</param>
        /// <returns></returns>
        public static string ReadFromINI(string filePath, string rootValue,string key,string defValue="")
        {
            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(rootValue, key, defValue, sb, 1024, filePath);
            return sb.ToString();
        }
        public static void WriteToINI(string filePath, string rootValue, string key, string newVal)
        {
           
            WritePrivateProfileString(rootValue, key, newVal, filePath);
           
        }
        
        /// <summary>
        /// 对INI文件进行读取操作
        /// </summary>
        /// <param name="IpAppName">表示INI文件内部根节点的值</param>
        /// <param name="IpKeyName">表示根节点下子标记的值</param>
        /// <param name="IpDefault">表示当标记值未设定或不存在时的默认值</param>
        /// <param name="IpReturnString">返回读取节点的值</param>
        /// <param name="nSize">读取的节点内容的最大容量</param>
        /// <param name="IpFileName">文件的全路径</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("kernel32")]
        static extern int GetPrivateProfileString(string IpAppName, string IpKeyName, string IpDefault, StringBuilder IpReturnString, int nSize, string IpFileName);
        /// <summary>
        /// 对INI文件进行写入操作
        /// </summary>
        /// <param name="mpAppName">INI文件内部根节点的值</param>
        /// <param name="mpKeyName">将要修改的标记名称</param>
        /// <param name="mpDefault">想要修改的内容</param>
        /// <param name="mpFileName">INI文件的全路径</param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("kernel32")]
        static extern long WritePrivateProfileString(string mpAppName, string mpKeyName, string mpDefault, string mpFileName);
    }

2,INI文件的扩展名为.ini 例如:System.ini 。

3,INI文件格式如下(使用记事本打开):

[ConnectString]
Data Source=192.168.1.97
DataBase=master
Uid=sa
Pwd=1

注解:

[ConnectString] :根节点,进行读写时首先需要知晓根节点值(例如 [ConnectString]  根节点值为:ConnectString)

DataBase:键,通过该键可获取该键对应的值,

到此这篇关于C#对INI文件进行读写操作的示例代码的文章就介绍到这了,更多相关C#读写操作INI内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文