本文实例讲述了C#实现利用Windows API读写INI文件的方法。分享给大家供大家参考。具体如下:
写入时,如果没有INI文件,自动创建INI
如果在创建时,GetLastError:5 检查IniPath是否添加了文件名称.ini
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"; } } }}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答