C#操作ini文件

   public class IniOperation
    {
        private static string iniFilePath;  //ini文件的路径
        const int MAXENTRY = 500000;               //最大缓冲值500*1000字节
        public IniOperation(string strIniFileName)
        {
            if (strIniFileName.Substring(2, 1) == "\\")//如果输入的为路径+Iin文件名,使用正则表达式会比较好一点
            { 
                iniFilePath = strIniFileName; 
            }
            else if (strIniFileName.Contains(".ini"))
            {
                string serciecName = AppconfigClass.GetValue("ServiceName");
                iniFilePath = string.IsNullOrEmpty(serciecName) ? strIniFileName : CommonClass.GetWindowsServiceInstallPath(serciecName) + strIniFileName;
            }
            else //i18n
            {
                iniFilePath = HttpContext.Current.Request.PhysicalApplicationPath + strIniFileName;
            }
        }

        #region 读写INI文件
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="IniKey"></param>
        /// <returns></returns>
        public string IniValue(string Section, string IniKey)
        {
            if (System.IO.File.Exists(iniFilePath))
            {
                System.IO.File.GetAccessControl(iniFilePath);
                StringBuilder temp = new StringBuilder(500);
                while (true)
                {
                    try
                    {
                        GetPrivateProfileString(Section, IniKey, "", temp, 500, iniFilePath);
                    }
                    catch (Exception ex)
                    {

                        System.Threading.Thread.Sleep(50);
                        break;
                    }
                    break;
                }
                return temp.ToString();
            }
            else
                return string.Empty;
        }
        /// <summary> 
        /// 写入INI文件 
        /// </summary> 
        /// <param name="Section">项目名称(如 [TypeName] )</param> 
        /// <param name="Key">键</param> 
        /// <param name="Value">值</param> 
        public void IniWriteValue(string Section, string Key, string Value)
        {
            if (System.IO.File.Exists(iniFilePath))
            {
                while (true)
                {
                    try
                    {
                        WritePrivateProfileString(Section, Key, Value, iniFilePath);
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(50);
                        return;
                    }
                    return;
                }
            }
            else
                return;
        }

        public string[] GetSectionNames()
        {
            if (System.IO.File.Exists(iniFilePath))
            {
                byte[] byBuff = new byte[MAXENTRY];
                GetPrivateProfileSectionNames(byBuff, MAXENTRY, iniFilePath);
                return Encoding.Default.GetString(byBuff).Trim('\0').Split('\0');
            }
            else
            {
                string[] strReturn = new string[0];
                return strReturn;
            }
        }

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileSectionNames(byte[] byRetBuff, int iSize, string filePath);

        #endregion

        static public string getXmlNodeValue(string filePath, string nodeName)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(filePath);
            XmlNodeList nodes = xmldoc.GetElementsByTagName("add");
            string strvalue = "";
            for (int i = 0; i < nodes.Count; i++)
            {
                XmlAttribute att = nodes[i].Attributes["key"];
                if (att.Value == nodeName)
                {
                    strvalue = nodes[i].Attributes["value"].Value;
                }

            }
            return strvalue;
        }

    }


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邹琼俊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值