欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實現(xiàn)利用Windows API讀寫INI文件的方法

 更新時間:2015年07月15日 15:05:01   作者:搶小孩糖吃  
這篇文章主要介紹了C#實現(xiàn)利用Windows API讀寫INI文件的方法,涉及C#針對ini文件的創(chuàng)建、讀取及寫入等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了C#實現(xiàn)利用Windows API讀寫INI文件的方法。分享給大家供大家參考。具體如下:

寫入時,如果沒有INI文件,自動創(chuàng)建INI
如果在創(chuàng)建時,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文件,自動創(chuàng)建INI
 /// 如果在創(chuàng)建時,GetLastError:5 檢查IniPath是否添加了文件名稱.ini
 /// </summary>
 public class INI
 {
  //聲明kernel32.dll函數(shù)
  [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>
  /// 構(gòu)造方法
  /// </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("配置文件創(chuàng)建成功");
    }
    else
    {
     Console.WriteLine("配置文件創(chuàng)建不成功");
    }
   }
   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#程序設(shè)計有所幫助。

相關(guān)文章

最新評論