zoukankan      html  css  js  c++  java
  • C# 读写ini配置文件(.net/SQL技术交流群206656202 入群需注明博客园)

    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Text;
    namespace Souxuexiao.Cache
    {
    
    public static class IniConfig
    {
    static string ConfigurationFilePath;
    
    static IniConfig()
    {
    var Ass = Assembly.GetExecutingAssembly();
    ConfigurationFilePath = Ass.CodeBase.Replace(System.IO.Path.GetExtension(Ass.CodeBase), ".ini").Replace(@"file:///", "");
    }
    
    public static string host
    {
    get
    {
    return Convert.ToString(GetValue("host", "127.0.0.1:5556"));
    }
    set
    {
    SetValue("host", value);
    }
    }
    public static string slave
    {
    get
    {
    return Convert.ToString(GetValue("slave", "127.0.0.1:5556"));
    }
    set
    {
    SetValue("slave", value);
    }
    }
    
    public static int WritePoolSize
    {
    get
    {
    return Convert.ToInt32(GetValue("WritePoolSize", "10"));
    }
    set
    {
    SetValue("WritePoolSize", value);
    }
    }
    public static int ReadPoolSize
    {
    get
    {
    return Convert.ToInt32(GetValue("ReadPoolSize", "10"));
    }
    set
    {
    SetValue("ReadPoolSize", value);
    }
    }
    
    static void SetValue(string keyName, object value)
    {
    NativeMethods.WritePrivateProfileString("redis", keyName, value.ToString(), ConfigurationFilePath);
    }
    
    static object GetValue(string keyName, object defaultValue)
    {
    StringBuilder retVal = new StringBuilder(1024);
    int i= NativeMethods.GetPrivateProfileString("redis", keyName, defaultValue.ToString(), retVal, 1024, ConfigurationFilePath);
    return retVal.ToString();
    }
    }
    
    class NativeMethods
    {
    [DllImport("kernel32")]
    internal static extern long WritePrivateProfileString(
    string appName,
    string keyName,
    string value,
    string fileName);
    
    [DllImport("kernel32")]
    internal static extern int GetPrivateProfileString(
    string appName,
    string keyName,
    string _default,
    StringBuilder returnedValue,
    int size,
    string fileName);
    
    }
    
    }

  • 相关阅读:
    结构-行为-样式-有趣的函数
    结构-行为-样式-angularJs笔记
    Js-Html 前端系列--页面撑开头尾
    Java 实现下载
    Js-Html 前端系列--Ajax
    Js-Html 前端系列--checkbox
    Nutz中过滤特殊字符
    NUTZ中处理系统未捕获异常
    Thymeleaf 笔记
    Json 使用小结
  • 原文地址:https://www.cnblogs.com/GodIsBoy/p/3724036.html
Copyright © 2011-2022 走看看