·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> C#操作INI文件

C#操作INI文件

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

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

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;

        }
 
    }
}