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.图片
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以尝试使用以下代码来读取包含中文的CSV文件,并解决乱码问题: ```csharp using System; using System.Collections.Generic; using System.IO; using System.Text; public class CsvReader { private string filePath; private string delimiter; public CsvReader(string filePath, string delimiter = ",") { this.filePath = filePath; this.delimiter = delimiter; } public List<List<string>> ReadCsvFile() { List<List<string>> csvData = new List<List<string>>(); using (var reader = new StreamReader(filePath, Encoding.Default)) { while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(delimiter); List<string> row = new List<string>(); foreach (var value in values) { row.Add(value); } csvData.Add(row); } } return csvData; } } public class Program { public static void Main(string[] args) { string filePath = "your_file_path.csv"; CsvReader csvReader = new CsvReader(filePath); List<List<string>> csvData = csvReader.ReadCsvFile(); // 打印CSV文件内容 foreach (var row in csvData) { foreach (var value in row) { Console.Write(value + "\t"); } Console.WriteLine(); } } } ``` 在上述代码中,我们使用了 `StreamReader` 类来读取CSV文件,并使用 `Encoding.Default` 来指定默认的编码。如果你的CSV文件采用其他编码方式,你可以根据实际情况进行调整。 请将 `your_file_path.csv` 替换为你的CSV文件的实际路径。此代码将CSV文件的内容打印到控制台,你可以根据需要进行后续处理。 希望这可以帮助到你!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值