C#实现利用Windows API读写INI文件的方法

本文实例讲述了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
{
 /// 
 /// 利用Windows API读写INI文件
 /// 写入时,如果没有INI文件,自动创建INI
 /// 如果在创建时,GetLastError:5 检查IniPath是否添加了文件名称.ini
 /// 
 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;
  /// 
  /// 构造方法
  /// 
  /// INI文件的绝对路径,后面不需要斜杠
  /// INI文件名称使用时不需要斜杠,需要.ini
  public INI(string INIPath,string INIFileName)
  {
   Console.WriteLine("INI Object building");
   IniPath = INIPath + "\\" + INIFileName;
   Console.WriteLine("INIFilePath :" + IniPath);
  }
  /// 
  /// 写入INI文件
  /// 
  /// Section
  /// Key
  /// Value
  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());
   }
  }
  /// 
  /// 读出INI文件
  /// 
  /// Section
  /// Key
  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);
  }
  /// 
  /// creat config file to application ini
  /// 
  /// 
  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());
   }
  }
  /// 
  /// write config for ip information
  /// 
  /// 
  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#程序设计有所帮助。

你可能感兴趣的:(C#实现利用Windows API读写INI文件的方法)