C#实现利用Windows API读写INI文件的方法
作者:抢小孩糖吃 时间:2023-03-16 01:23:06
本文实例讲述了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#程序设计有所帮助。
标签:C#,INI
0
投稿
猜你喜欢
Java编写迷宫小游戏
2021-08-06 12:53:33
SpringBoot 创建web项目并部署到外部Tomcat
2023-09-15 18:25:04
浅谈Java后台对JSON格式的处理操作
2023-02-16 07:28:36
JAVA并发图解
2023-03-26 03:11:41
C#中out参数、ref参数与值参数的用法及区别
2022-10-16 17:15:39
三行Android代码实现白天夜间模式流畅切换
2021-06-11 08:15:12
一篇文章带你了解C语言二分查找
2023-10-16 19:51:57
Java如何使用httpclient检测url状态及链接是否能打开
2022-07-03 21:23:25
C语言运算符及其优先级汇总表口诀
2021-12-12 21:14:27
Java中方法重写与重载的区别
2022-07-25 04:49:01
一篇文章弄懂Java8中的时间处理
2022-07-22 07:56:32
深入理解Java虚拟机 JVM 内存结构
2022-05-01 20:49:57
List调用toString()方法后,去除两头的中括号实例
2023-09-28 11:18:56
简单了解Java编程中抛出异常的方法
2023-08-07 17:31:49
Struts2学习笔记(5)-参数传递方法
2023-08-30 13:03:21
Android手势识别器GestureDetector使用详解
2022-01-16 14:25:17
C#基于正则表达式删除字符串中数字或非数字的方法
2021-07-12 17:27:44
android ToolBar的简单使用
2023-03-05 10:44:33
c#之滚动字幕动画窗体的实现详解
2022-02-14 08:46:19
C#自定义缓存封装类实例
2023-10-15 09:24:27