国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2020-01-24 01:37:44
字體:
供稿:網(wǎ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 + "http://" + 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è)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 孝义市| 宁国市| 铜山县| 合江县| 乌鲁木齐市| 高淳县| 镇康县| 元氏县| 察雅县| 太和县| 嵊泗县| 新干县| 永胜县| 梧州市| 上犹县| 南乐县| 常山县| 乐都县| 彩票| 常德市| 桐城市| 嘉黎县| 全椒县| 毕节市| 霍城县| 梅河口市| 温泉县| 台安县| 咸宁市| 襄城县| 托克托县| 肇东市| 会泽县| 武隆县| 碌曲县| 柘荣县| 平昌县| 贺兰县| 寿阳县| 隆尧县| 新源县|