C#完成應用Windows API讀寫INI文件的辦法。本站提示廣大學習愛好者:(C#完成應用Windows API讀寫INI文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是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
{
/// <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#法式設計有所贊助。