[CSharpTips]C# 读写INI文件
2022/9/2 1:24:25
本文主要是介绍[CSharpTips]C# 读写INI文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
读写.ini文件工具 可以直接使用
using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace WinFormSharpDemo.Common.Helpers { /// <summary> /// INI文件读写工具 /// </summary> public static class IniHelper { /// <summary> /// 为INI文件中指定的节点取得字符串 /// </summary> /// <param name="lpAppName">欲在其中查找关键字的节点名称</param> /// <param name="lpKeyName">欲获取的项名</param> /// <param name="lpDefault">指定的项没有找到时返回的默认值</param> /// <param name="lpReturnedString">指定一个字串缓冲区,长度至少为nSize</param> /// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param> /// <param name="lpFileName">INI文件完整路径</param> /// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符</returns> [DllImport("kernel32")] private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); /// <summary> /// 修改INI文件中内容 /// </summary> /// <param name="lpApplicationName">欲在其中写入的节点名称</param> /// <param name="lpKeyName">欲设置的项名</param> /// <param name="lpString">要写入的新字符串</param> /// <param name="lpFileName">INI文件完整路径</param> /// <returns>非零表示成功,零表示失败</returns> [DllImport("kernel32")] private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName); /// <summary> /// 获取所有节点 /// </summary> /// <param name="section"></param> /// <param name="key"></param> /// <param name="def"></param> /// <param name="retVal"></param> /// <param name="size"></param> /// <param name="filePath"></param> /// <returns></returns> [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")] private static extern uint GetPrivateProfileStringA(string section, string key, string def, Byte[] retVal, int size, string filePath); /// <summary> /// 获取所有节点 /// </summary> /// <param name="iniFilename">文件名</param> /// <returns></returns> public static List<string> ReadSections(string iniFilename) { List<string> result = new List<string>(); Byte[] buf = new Byte[65536]; uint len = GetPrivateProfileStringA(null, null, null, buf, buf.Length, iniFilename); int j = 0; for (int i = 0; i < len; i++) if (buf[i] == 0) { result.Add(Encoding.Default.GetString(buf, j, i - j)); j = i + 1; } return result; } /// <summary> /// 读取INI文件值 /// </summary> /// <param name="section">节点名</param> /// <param name="key">键</param> /// <param name="def">未取到值时返回的默认值</param> /// <param name="filePath">INI文件完整路径</param> /// <returns>读取的值</returns> public static string Read(string section, string key, string def, string filePath) { StringBuilder sb = new StringBuilder(1024); GetPrivateProfileString(section, key, def, sb, 1024, filePath); return sb.ToString(); } /// <summary> /// 写INI文件值 /// </summary> /// <param name="section">欲在其中写入的节点名称</param> /// <param name="key">欲设置的项名</param> /// <param name="value">要写入的新字符串</param> /// <param name="filePath">INI文件完整路径</param> /// <returns>非零表示成功,零表示失败</returns> public static int Write(string section, string key, string value, string filePath) { if (!Directory.Exists(Path.GetDirectoryName(filePath))) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } return WritePrivateProfileString(section, key, value, filePath); } /// <summary> /// 删除节 /// </summary> /// <param name="section">节点名</param> /// <param name="filePath">INI文件完整路径</param> /// <returns>非零表示成功,零表示失败</returns> public static int DeleteSection(string section, string filePath) { return Write(section, null, null, filePath); } /// <summary> /// 删除键的值 /// </summary> /// <param name="section">节点名</param> /// <param name="key">键名</param> /// <param name="filePath">INI文件完整路径</param> /// <returns>非零表示成功,零表示失败</returns> public static int DeleteKey(string section, string key, string filePath) { return Write(section, key, null, filePath); } } }
这篇关于[CSharpTips]C# 读写INI文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具