zoukankan      html  css  js  c++  java
  • C#操作程序配置文件(ini)辅助类

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Runtime.InteropServices;
     6 using System.IO;
     7 
     8 namespace CommonLibrary
     9 {
    10 
    11     public class ConfigHelper
    12     {
    13         public string iniPath;
    14         /// <summary>
    15         /// 写入ini文件的函数
    16         /// </summary>
    17         /// <param name="section">段落</param>
    18         /// <param name="key">以单词开头的关键字</param>
    19         /// <param name="value"> 关键字值</param>
    20         /// <param name="filePath">INI文件的完整路径和名称</param>
    21         /// <returns></returns>
    22         [DllImport("Kernel 32")]
    23         private static extern long WritePrivateProfileString(String section, String key, string value, String filePath);
    24 
    25         /// <summary>
    26         /// ini文件读操作函数
    27         /// </summary>
    28         /// <param name="section">段落</param>
    29         /// <param name="key">以单词开头的关键字</param>
    30         /// <param name="def">无法读取时候时候的缺省数值</param>
    31         /// <param name="refValue">读取数值</param>
    32         /// <param name="size">数值的大小</param>
    33         /// <param name="filepath">INI文件的完整路径和名称</param>
    34         /// <returns></returns>
    35         [DllImport("kernel32")]
    36         private static extern int GetPrivateProfileString(String section, String key, String def, StringBuilder refValue, int size, String filepath);
    37 
    38         public ConfigHelper(String filePath)
    39         {
    40             iniPath = filePath;
    41         }
    42         /// <summary>
    43         /// 写入ini文件
    44         /// </summary>
    45         /// <param name="section">段落</param>
    46         /// <param name="key">关键字</param>
    47         /// <param name="value">关键字值</param>
    48         public void WriteIni(String section,String key,string value)
    49         {
    50             WritePrivateProfileString(section, key, value, this.iniPath);
    51         }
    52         /// <summary>
    53         /// 读取ini文件
    54         /// </summary>
    55         /// <param name="selection">段落</param>
    56         /// <param name="key">关键字</param>
    57         /// <returns></returns>
    58         public String GetInn(String selection, String key)
    59         {
    60             StringBuilder temp = new StringBuilder(500);
    61             Int32 i = GetPrivateProfileString(selection, key, "", temp, 500, this.iniPath);
    62             return temp.ToString();
    63         }
    64         /// <summary>
    65         /// 验证文件是否存在
    66         /// </summary>
    67         /// <returns></returns>
    68         public bool ExistsFile()
    69         {
    70             return File.Exists(this.iniPath);
    71         }
    72     }
    73 }

    因为自己写的小工具中需要保存配置文件,因此做一个备注

  • 相关阅读:
    Linux下PHP升级的方法
    centos6 授权文件夹所有用户可用
    重置密码遇到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor:yes)问题
    MySQL Daemon failed to start. 正在启动 mysqld:[失败]
    MySql取消密码强度验证功能
    twbsPagination.js分页插件
    同一个Tomcat部署多个springboot项目问题
    同一个tomcat部署多个项目导致启动失败
    启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback
    Vue中关于vue-awesome-swiper插件使用以及要注意的 “坑”
  • 原文地址:https://www.cnblogs.com/acoll/p/3320522.html
Copyright © 2011-2022 走看看