求一个C# 读写ini配置文件 的源码
下面这个例子就比较好,但我下载不下来,不知道为什么?
http://hi.baidu.com/zabbix/blog/item/f26c4b83ffd72c3ac75cc35f.html
谢谢大家!请发我邮箱 [email protected] 

解决方案 »

  1.   


    using System;
    using System.Text;
    using System.IO;
    using System.Runtime.InteropServices;namespace PubOp
    {
        public class OperateIniFile
        {
            API函数声明#region API函数声明        [DllImport("kernel32")]//返回0表示失败,非0为成功
            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);
            #endregion        读Ini文件#region 读Ini文件        public static string ReadIniData(string Section,string Key,string NoText,string iniFilePath)
            {
                if(File.Exists(iniFilePath))
                {
                    StringBuilder temp = new StringBuilder(1024);
                    GetPrivateProfileString(Section,Key,NoText,temp,1024,iniFilePath);
                    return temp.ToString();
                }
                else
                {
                    return String.Empty;
                }
            }        #endregion        写Ini文件#region 写Ini文件        public static bool WriteIniData(string Section,string Key,string Value,string iniFilePath)
            {
                if(File.Exists(iniFilePath))
                {
                    long OpStation = WritePrivateProfileString(Section,Key,Value,iniFilePath);    
                    if(OpStation == 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }        #endregion
        }
    }参考 
    http://kb.cnblogs.com/page/43446/
      

  2.   

    1,这没什么难的,直接使用kernel32.dll组件提供的WritePrivateProfileString和GetPrivateProfileString写和读就行了
    2,现在基本淘汰ini配置文件了,一般都是用xml来保存配置
    最简单的读写ini的方法:using System;
    using System.Runtime.InteropServices;
    using System.Text;namespace Ini
    {
        /// <summary>
        /// Create a New INI file to store or load data
        /// </summary>
        public class IniFile
        {
            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);        /// <summary>
            /// INIFile Constructor.
            /// </summary>
            /// <PARAM name="INIPath"></PARAM>
            public IniFile(string INIPath)
            {
                path = INIPath;
            }
            /// <summary>
            /// Write Data to the INI File
            /// </summary>
            /// <PARAM name="Section"></PARAM>
            /// Section name
            /// <PARAM name="Key"></PARAM>
            /// Key Name
            /// <PARAM name="Value"></PARAM>
            /// Value Name
            public void IniWriteValue(string Section,string Key,string Value)
            {
                WritePrivateProfileString(Section,Key,Value,this.path);
            }
            
            /// <summary>
            /// Read Data Value From the Ini File
            /// </summary>
            /// <PARAM name="Section"></PARAM>
            /// <PARAM name="Key"></PARAM>
            /// <PARAM name="Path"></PARAM>
            /// <returns></returns>
            public string IniReadValue(string Section,string Key)
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section,Key,"",temp, 
                                                255, this.path);
                return temp.ToString();        }
        }
    }
      

  3.   

    另附2个网站供参考:
    http://xianfeixie.blog.163.com/blog/static/1686523622010112231630130/
    http://www.cnblogs.com/chiropter/articles/1597918.html
    如果是你的项目自己用,我建议还是改为xml作为配置文件吧