C# 控制台程序输出乱码

前面概要
  • 首先有几个问题
  • 因为项目需要,和一个控制台程序做链接,控制台那边会输出用户选择的图片路径。
  • 但是我发现如果图片路径是中文,就会乱码,报错
  • 结果只需要把编码全部设置成utf-8就可以了
  • 注意Console 输出为utf-8
csharp 复制代码
Console.OutputEncoding = Encoding.UTF8;
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\"; // 设置初始目录为c盘
                openFileDialog.Filter = "图片文件(*.jpg;*.jpeg;*.png)|*.jpg;*.jpeg;*.png"; // 设置过滤选项,只显示图片文件
                openFileDialog.FilterIndex = 1;
                openFileDialog.RestoreDirectory = true;
                openFileDialog.Multiselect = true;
                if (openFileDialog.ShowDialog() == DialogResult.OK) // 显示对话框
                {
                    // 用户选择了文件
                    var resutl = string.Join("|", openFileDialog.FileNames);
                    Console.WriteLine(resutl); 
                }
            }
  • 启动信息也要设置成utf-8
csharp 复制代码
string filePathv = Application.dataPath + "/Other/SelectImage.exe";
        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            FileName = filePathv,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            StandardOutputEncoding = System.Text.Encoding.UTF8,
        };
相关推荐
小小鱼儿飞1 天前
QT音乐播放器18----新歌速递播放、隐藏顶部和底部工具栏、自定义ToolTips
开发语言·qt
穆雄雄1 天前
Rust 程序适配 OpenHarmony 实践:以 sd 工具为例
开发语言·rust·harmonyos
0***141 天前
Swift资源
开发语言·ios·swift
z***I3941 天前
Swift Tips
开发语言·ios·swift
J***Q2921 天前
Swift Solutions
开发语言·ios·swift
铅笔小新z1 天前
C++入门指南:开启你的编程之旅
开发语言·c++
小满、1 天前
MySQL :实用函数、约束、多表查询与事务隔离
数据库·mysql·事务·数据库函数·多表查询
Gavin-Wang1 天前
Swift + CADisplayLink 弱引用代理(Proxy 模式) 里的陷阱
开发语言·ios·swift
百***35331 天前
PostgreSQL_安装部署
数据库·postgresql