首页 > Windows开发 > 详细

c#读写ini文件

时间:2016-01-22 21:05:34      阅读:363      评论:0      收藏:0      [点我收藏+]

通过调用win32 api可以直接读写ini文件

写了一个助手类方便操作:

技术分享
using System;
using System.Text;
using System.Runtime.InteropServices;

public static class IniFileHelper
{
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    public static void Write(string section, string key, string val, string filePath)
    {
        WritePrivateProfileString(section, key, val, filePath);
    }

    public static string Read(string section, string key, string filePath)
    {
        var stringBuilder = new StringBuilder(255);
        GetPrivateProfileString(section, key, string.Empty, stringBuilder, 255, filePath);
        return stringBuilder.ToString();
    }
}
IniFileHelper

 

测试代码:

static void Main(string[] args)
{
    IniFileHelper.Write("BaseSetting", "Language", "en_us", "./Config.ini");
    IniFileHelper.Write("Sound", "Enable", "True", "./Config.ini");
    IniFileHelper.Write("Sound", "Volume", "100", "./Config.ini");
}

 

 

运行结果:

技术分享

 

 

读取测试:

技术分享

c#读写ini文件

原文:http://www.cnblogs.com/hont/p/5151984.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!