zoukankan      html  css  js  c++  java
  • C#使用ini配置文件保存读取配置数据

    使用ini配置项目的好处:

    1. 文本保存便于查看和修改
    2. 配置过程简单高效
    3. 使用、扩展灵活

    实例:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;
    
    namespace leisai1
    {
        public partial class Settings : Component
        {
    
            //配置文件路径,可以扩展做成多配置文件
            private static string IniFilePath = "D:/workspace/vs2013/C#学习/kongzhika/leisai1/Config.ini";
    
            public Settings()
            {
                InitializeComponent();
            }
    
            public Settings(IContainer container)
            {
                container.Add(this);
    
                InitializeComponent();
            }
    
            [DllImport("kernel32.dll")]
            private static extern long WritePrivateProfileString(string section, string key, string value, string filepath);
     
            [DllImport("kernel32.dll")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder returnvalue, int buffersize, string filepath);
    
            //公开接口:读取配置
            public static void GetValue(string section, string key, out string value)
            {
    
                StringBuilder stringBuilder = new StringBuilder();
                GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath);
                value = stringBuilder.ToString()??"";
            }
    
            //公开接口:设置配置
            public static void SetValue(string section, string key, string value)
            {
                WritePrivateProfileString(section, key, value, IniFilePath);
            }
        }
    }
        
    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    二:数组去重
    一:JS 数组对象根据相同key合并成新的数组对象(将一维数组转为多维数组)
    滑动scroll没有效果
    品字布局占满全屏
    js计算器
    html样式初始化
    js计算器
    js邮箱验证
    js菱形
    js实现金字塔图形
  • 原文地址:https://www.cnblogs.com/yamboo/p/13763769.html
Copyright © 2011-2022 走看看