前面概要
- 首先有几个问题
- 因为项目需要,和一个控制台程序做链接,控制台那边会输出用户选择的图片路径。
- 但是我发现如果图片路径是中文,就会乱码,报错
- 结果只需要把编码全部设置成
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,
};