码迷,mamicode.com
首页 > 其他好文 > 详细

C# 操作INI文件

时间:2014-07-12 09:14:40      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:blog   使用   文件   art   cti   for   

最近用到保存文件的相关东西,主要是封装两个函数,代码如下:可以直接使用

using System;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace IniFileTest
{
    public class INI
    {

        const int COUNT = 0xFFFF;
        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);


        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] buffer, int size, string filePath);


        /// <summary>
        /// ini关联的文件
        /// </summary>
        private string FileName;

        public INI(string filename)
        {
            FileInfo file = new FileInfo(filename);
            if (file.Exists)
            {
                FileName = new FileInfo(filename).FullName;
            }
        }

        /// <summary>
        /// 删除Section
        /// </summary>
        /// <param name="SectionName"></param>
        /// <returns></returns>
        public bool EraseSection(string SectionName)
        {
            return WritePrivateProfileString(SectionName, null, null, FileName);

        }

        /// <summary>
        /// 写入section 键值对,如果没有section 则创建并写入
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Write(string section, string key, string value)
        {

            return WritePrivateProfileString(section, key, value, FileName);
        }

        /// <summary>
        /// 删除键
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool EraseKey(string section, string key)
        {
            return WritePrivateProfileString(section, key, null, FileName);
        }


        /// <summary>
        /// 获取所有section
        /// </summary>
        /// <returns></returns>
        public StringCollection GetSections()
        {
            StringCollection sections = new StringCollection();
            byte[] buffer = new byte[COUNT];
            int length = GetPrivateProfileString(null, null, null, buffer, COUNT, FileName);
            int start = 0;
            for (int i = 0; i < length; i++)
            {
                if (buffer[i] == 0 && i > start)
                {
                    string str = Encoding.Default.GetString(buffer, start, i - start);
                    sections.Add(str);
                    start = i + 1; ;
                }
            }
            return sections;
        }


        /// <summary>
        /// 获取section下的所哟keys
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public StringCollection GetSectionKeys(string section)
        {
            StringCollection keys = new StringCollection();
            byte[] buffer = new byte[COUNT];
            int length = GetPrivateProfileString(section, null, null, buffer, COUNT, FileName);
            int start = 0;
            for (int i = 0; i < length; i++)
            {
                if (buffer[i] == 0 && i > start)
                {
                    string str = Encoding.Default.GetString(buffer, start, i - start);
                    keys.Add(str);
                    start = i + 1; ;
                }
            }
            return keys;
        }

        /// <summary>
        /// 查寻指定的key的值,没有则返回defaultvalue
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="defaultvalue"></param>
        /// <returns></returns>
        public String GetValue(String section, string key, string defaultvalue)
        {
            byte[] bytes = new byte[COUNT];
            int s = GetPrivateProfileString(section, key, defaultvalue, bytes, COUNT, FileName);
            string str = Encoding.GetEncoding(0).GetString(bytes, 0, s);
            return defaultvalue;

        }
 
    }
}

  

C# 操作INI文件,布布扣,bubuko.com

C# 操作INI文件

标签:blog   使用   文件   art   cti   for   

原文地址:http://www.cnblogs.com/shenfuqiang/p/3832689.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!