zoukankan      html  css  js  c++  java
  • INI 文件的读写操作

    在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下:

      #region  变量
    
            private  static readonly string strFilePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "App.ini";//INI文件路径
    
            #endregion 
    
            #region  私有方法
    
            /// <summary>
            /// 写入INI文件
            /// </summary>
            /// <param name="section">节点名称[如[TypeName]]</param>
            /// <param name="key">键</param>
            /// <param name="val">值</param>
            /// <param name="filepath">文件路径</param>
            /// <returns></returns>
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
    
    
            /// <summary>
            /// 读取INI文件
            /// </summary>
            /// <param name="section">节点名称</param>
            /// <param name="key">键</param>
            /// <param name="def">值</param>
            /// <param name="retval">stringbulider对象</param>
            /// <param name="size">字节大小</param>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);
    
    
            #endregion
    
            #region  公共方法 
    
    
            public  static string GetValue(string section, string key)
            {
                try
                {
                    int size = 2048;
                    StringBuilder temp = new StringBuilder(size);
                    GetPrivateProfileString(section, key, "", temp, size, strFilePath);
                    return temp.ToString();
                }
                catch(Exception e)
                {
                    throw new Exception(e.Message);
                }
    
            }
    
            public static bool WriteValue( string section, string key, string value)
            {
                try
                {
                   long length=  WritePrivateProfileString(section, key, value, strFilePath);
                    return length>0;
                }
                catch(Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
    
    
            #endregion
    

      

  • 相关阅读:
    英语语法入门十五(名词所有格)
    在线调试Arduino
    CANopen和Canfestival
    嵌入式系统中的printf
    云原生爱好者周刊:Lens 5.0 发布,更炫、更快、更强!
    基于 KubeSphere 的 Nebula Graph 多云架构管理实践
    KubeSphere Meetup 北京站火热报名中 | 搭载 CIC 2021 云计算峰会
    KubeSphere Helm 应用仓库源码分析
    开启 Calico eBPF 数据平面实践
    KubeSphere 在直播应用中的实践
  • 原文地址:https://www.cnblogs.com/wisdo/p/5720596.html
Copyright © 2011-2022 走看看