zoukankan      html  css  js  c++  java
  • .net 操作INI文件

    using System.Runtime.InteropServices;
    using System.Text;
    
    namespace FaureciaManager
    {
        public class FileINI
        {
            /// <summary>
            /// 写操作
            /// </summary>
            /// <param name="section"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            [DllImport("Kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
    
            /// <summary>
            /// 读操作
            /// </summary>
            /// <param name="section"></param>
            /// <param name="key"></param>
            /// <param name="defValue">未读取到的默认值</param>
            /// <param name="retvalue">读取到的值</param>
            /// <param name="size">大小</param>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            [DllImport("Kernel32")]
            private static extern long GetPrivateProfileString(string section, string key, string defValue, StringBuilder retvalue, int size, string filePath);
    
            /// <summary>
            /// 读INI文件
            /// </summary>
            /// <param name="section"></param>
            /// <param name="key"></param>
            /// <param name="defValue">未读取到时候的默认值</param>
            /// <param name="filePath">文件路径</param>
            /// 用法 FileINI.ReadIni("ConfigURL", "URL", "192.168.10.128:7500", ConfigFilePath);
            public static StringBuilder ReadIni(string section, string key, string defValue, string filePath)
            {
                StringBuilder retValue = new StringBuilder();
                 GetPrivateProfileString(section, key, defValue, retValue, 256, filePath);
                 return retValue;
            }
    
            /// <summary>
            /// 写INI文件
            /// </summary>
            /// <param name="section"></param>
            /// <param name="key"></param>
            /// <param name="value"></param>
            /// <param name="filePath">文件路径</param>
            /// 用法  FileINI.WriteIni("ConfigURL", "URL", this.tbxUrl.Text.Trim(), ConfigFilePath);
            public static long WriteIni(string section, string key, string value, string filePath)
            {
                return WritePrivateProfileString(section, key, value, filePath);
            }
    
            /// <summary>
            /// 删除节
            /// </summary>
            /// <param name="section"></param>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            public static long DeleteSection(string section, string filePath)
            {
                return WritePrivateProfileString(section, null, null, filePath);
            }
    
            /// <summary>
            /// 删除键
            /// </summary>
            /// <param name="section"></param>
            /// <param name="key"></param>
            /// <param name="filePath">文件路径</param>
            /// <returns></returns>
            public static long DeleteKey(string section, string key, string filePath)
            {
                return WritePrivateProfileString(section, key, null, filePath);
            }
        }
    }
  • 相关阅读:
    Oracle->oracle单实例Shell脚本[20180122]
    oracle 11g grid软件安装[20180121]
    MySQL-5.7.20主从复制测试[20180110]
    Sonarqube中文插件-Linux[20180105]
    Sonar安装-Linux[20171227]
    (Linux 日常命令)[20171225]
    ThreadLocal的实现机制
    LruCache源码分析
    命令提示符怎么以管理员方式打开
    Eslint 从入门到放弃 http://blog.csdn.net/fay462298322/article/details/74931092
  • 原文地址:https://www.cnblogs.com/randyzhuwei/p/4058740.html
Copyright © 2011-2022 走看看