c#使用Windows API读写INI文件代码

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

利用Windows API读写INI文件写入时,如果没有INI文件,自动创建INI;如果在创建时,GetLastError返回5,则检查IniPath是否添加了文件名称.ini

c#代码

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

namespace NameSpace
{
    /// <summary>
    /// 利用Windows API读写INI文件
    /// 写入时,如果没有INI文件,自动创建INI
    /// 如果在创建时,GetLastError:5 检查IniPath是否添加了文件名称.ini
    /// </summary>
    public class INI
    {
        //声明kernel32.dll函数
        [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);
        //
        [DllImport("kernel32")]
        public static extern uint GetLastError();

        string IniPath = null;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="INIPath">INI文件的绝对路径,后面不需要斜杠</param>
        /// <param name="INIFileName">INI文件名称使用时不需要斜杠,需要.ini</param>
        public INI(string INIPath,string INIFileName)
        {
            Console.WriteLine("INI Object building");
            IniPath = INIPath + "\\" + INIFileName;
            Console.WriteLine("INIFilePath :" + IniPath);
        }

        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="Section">Section</param>
        /// <param name="Key">Key</param>
        /// <param name="Value">Value</param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            Console.WriteLine("---IniWriteValue---");
            Console.WriteLine("Section :" + Section);
            Console.WriteLine("Key :" + Key);
            Console.WriteLine("Value :" + Value);
            Console.WriteLine("IniPath :" + IniPath);
            UInt32 Snapshot = GetLastError();
            //
            WritePrivateProfileString(Section, Key, Value, IniPath);
            if (Snapshot != GetLastError())
            {
                Console.WriteLine("GetLastError :" + GetLastError());
            }
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="Section">Section</param>
        /// <param name="Key">Key</param>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder result = new StringBuilder(256);
            GetPrivateProfileString(Section, Key, null, result, 256, IniPath);
            return result.ToString();
        }

        public bool ExistINIFile()
        {
            return File.Exists(IniPath);
        }

        /// <summary>
        /// creat config file to application ini
        /// </summary>
        /// <param name="dnf_path"></param>
        public void CreateConfig(string IP)
        {
            Console.WriteLine("CreateConfig");
            Console.WriteLine("IP:" + IP);
            try
            {
                WriteConfigIP(IP);

                if (ExistINIFile())
                {
                    Console.WriteLine("配置文件创建成功");
                }
                else
                {
                    Console.WriteLine("配置文件创建不成功");
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("出错信息:" + err.ToString());
            }
        }

        /// <summary>
        /// write config for ip information
        /// </summary>
        /// <param name="IP"></param>
        public void WriteConfigIP(string IP)
        {
            string Section = "Config";
            string Key = "IP";
            string Value = IP;
            try
            {
                IniWriteValue(Section, Key, Value);
            }
            catch (Exception err)
            {
                Console.WriteLine("出错信息:" + err.ToString());
            }
        }

        public string ReadConfigIP()
        {
            try
            {
                string Section = "Config";
                string result = IniReadValue(Section, "IP");
                Console.WriteLine("ReadConfigIP Result :" + result);
                return result;
            }
            catch (Exception err)
            {
                Console.WriteLine("出错信息:" + err.ToString());
                return "Read Error";
            }
        }

    }
}

标签: 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:C#实现SAFER+加密

下一篇:C#使用正则判断字符串是否是Guid