首页 > 编程语言 >C#使用utf-8读取ini

C#使用utf-8读取ini

时间:2023-10-09 19:24:44浏览次数:40  
标签:key utf string C# ini encodingName byte getBytes

参考: c#使用指定编码格式读写ini

IniFile.cs

using System.Runtime.InteropServices;
class IniFile { public IniFile(string filePath) { m_FilePath = filePath; } private string m_FilePath; [DllImport("kernel32")] private static extern bool WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath); //与ini交互必须统一编码格式 private byte[] getBytes(string s, string encodingName) { return null == s ? null : Encoding.GetEncoding(encodingName).GetBytes(s); } public string ReadString(string section, string key, string defaultVal = "", int size = 1024, string encodingName = "utf-8") { byte[] buffer = new byte[size]; int count = GetPrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(defaultVal, encodingName), buffer, size, m_FilePath); return Encoding.GetEncoding(encodingName).GetString(buffer, 0, count).Trim(); } public bool WriteString(string section, string key, string value, string encodingName = "utf-8") { return WritePrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(value, encodingName), m_FilePath); } }

测试:

IniFile iniGate = new IniFile("E:\\test.ini");
string Id = iniGate.ReadString("server", "Id");
string LineIp = iniGate.ReadString("server", "LineIp");
string Ip = iniGate.ReadString("address", "Ip");

iniGate.WriteString("server", "title", "五线");test.ini

test.ini

[server]
Id        = 3
title        = 一线

[address]
Ip            = localhost

 

标签:key,utf,string,C#,ini,encodingName,byte,getBytes
From: https://www.cnblogs.com/barrysgy/p/17752928.html

相关文章

  • C# 元组的demo
      //Seehttps://aka.ms/new-console-templateformoreinformationConsole.WriteLine("Hello,World!");stringname="马良";intage=16;vara=(Name:name,Age:age,1,2,3,4,5,6,7,8,9,0);(stringa1,inta2)=(name,age);varun......
  • [BJDCTF2020]ZJCTF,不过如此
    原理关于preg_replace\e的代码执行双引号和单引号的区别可变变量解题过程代码审计<?phperror_reporting(0);$text=$_GET["text"];$file=$_GET["file"];if(isset($text)&&(file_get_contents($text,'r')==="Ihaveadream")){echo......
  • LCT板子
    //我坚信LCT可以平替树剖#include<bits/stdc++.h>#definelst[o].ch[0]#definerst[o].ch[1]#defineintlonglongusingnamespacestd;constintN=500010;constintinf=1e9;intread(){intx=0,f=1;charch=getchar();while(ch<48||ch>57......
  • Win10安装VSCode并配置Python环境(完美避开踩过的所有坑)
    安装VScode下载vscode下载链接:https://code.visualstudio.com/Download根据自己的电脑型号下载对应的版本。我下载的是windows/UserInstaller,但是使用时会提示“”。所以,推荐下载SystemInstaller版本。两者区别可以自行百度,或......
  • uniCloud-用db schema在客户端访问数据库
    先下载所有dbschema在前端代码中直接访问数据库表拿数据index.vue<template> <viewclass="content"> <viewv-for="itemindataList":key="item._id"> {{item.name}}---{{item.gender}}---{{item.age}} </view> <......
  • 死锁和Lock锁
    死锁就是两个线程都有着一个对象的锁 然后下一步都想去拿另外一个线程的锁,因为两个线程有的锁还没解开,形成循环僵持,谁都想要另外一个线程的锁,但是又没解开自己拿到的锁。 解决办法示例: 就是可以等另外一个线程解开了锁然后再去拿锁。 Lock锁:和synchonized锁是一样的......
  • Typecho博客网站迁移:MySQL ➡️ MarialDB
    目录1.引言2.Typecho的自定义配置迁移3.数据库迁移:MySQL->MarialDB3.1在原服务器中备份并导出数据库文件3.2将“backupdb.sql”文件拷贝至新服务器并导入数据4.Nginx配置5.Handsome主题操作1.引言由于服务、价格等因素更换云服务器是很常见的情况,本文记录了Typecho博......
  • VS code+python环境部署
    安装VScode下载vscode下载链接:https://code.visualstudio.com/Download根据自己的电脑型号下载对应的版本。我下载的是windows/UserInstaller,但是使用时会提示“”。所以,推荐下载SystemInstaller版本。两者区别可以自行百度,或......
  • C# 操作防火墙
    https://www.cnblogs.com/code1992/p/11661078.html   ///<summary>///通过对象防火墙操作///</summary>///<paramname="isOpenDomain">域网络防火墙(禁用:false;启用(默认):true)</param>///<paramname="isOpenPublicState">公......
  • Docker 本地化镜像导入导出(针对服务器无法连接外网进行更新)
    镜像在开发公网测试环境中是可以下载的,但在某些生产环境中是无法访问外网进行镜像的拉取。这时候就只能在测试或者开发环境先保存镜像,然后复制到生产的机器,然后加载到生产本地机器,下面就来介绍下该如何操作。1.下载镜像到本地dockerpullregistry.cn-shenzhen.aliyuncs.com......