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 }

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

  • 相关阅读:
    【问题 & 解决】git 克隆项目时超时
    【荐】开源Winform控件库:花木兰控件库
    virtualenv Python 项目环境搭建必备
    英文电影评论情感分析
    中华古诗词知识图谱构建之数据获取
    基于Word2vec的诗词多情感分析
    中华古诗词知识图谱之网页设计及实现(完结)
    1211诗词时空背景
    1217诗词鉴赏
    中华古诗词知识图谱之实体关系构建&导入neo4j数据库
  • 原文地址:https://www.cnblogs.com/acoll/p/3320522.html
Copyright © 2011-2022 走看看