C# 读写配置文档容易出现乱码的情况解决

C# 读写配置文档容易出现乱码的情况解决

1.上一篇博客提到了c#读写代码,但是未考虑到中文情况,运用上一篇博客提到的方法会造成读写中文都会出现乱码的情况。
2.参考博客:C# 读写INI文件中文乱码问题
3.直接附上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cfg
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);


        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(string section, byte[] key, byte[] val, string filePath);
        private void button1_Click(object sender, EventArgs e)
        {
            string path = Application.StartupPath + @"\app.cfg";//配置文档中的地址
            byte[] Buffer = new byte[1024];
            int bufLen = GetPrivateProfileString("SetpName", "FlowStart", "", Buffer, 1024, path);//读配置文档
            string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);
            Console.WriteLine(s);

            WritePrivateProfileString("General", Encoding.UTF8.GetBytes("Port"), Encoding.UTF8.GetBytes("你好明天"), path);//写配置文档
        }
    }
}

4.图片
在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值