process.standardInput编码问题

我对process.standartInput编码的编码有问题。 我在我的Windows窗体应用程序中使用了一些进程,但输入应该是UTF-8。 Process.StandardInput.Encoding是只读的,因此我无法将其设置为UTF-8,并且它获得了Windows默认编码,这会恶化UTF-8中的原生字符。 程序中使用了2个进程,一个将输出写入文件和其他读取。 因为我可以将输出编码设置为UTF-8,这部分工作正常,但回读是我遇到问题的部分。 我将包括我使用该过程的部分。

ProcessStartInfo info = new ProcessStartInfo("mysql"); info.RedirectStandardInput = true; info.RedirectStandardOutput = false; info.Arguments = mysqldumpstring; info.UseShellExecute = false; info.CreateNoWindow = true; Process p1 = new Process(); p1.StartInfo = info; p1.Start(); string res = file.ReadToEnd(); file.Close(); MessageBox.Show(p1.StandardInput.Encoding.EncodingName); //= where encoding should be Encoding.UTF8; p1.StandardInput.WriteLine(res); p1.Close(); 

使用StreamWriter创建下一个方法(而不是StandardInput)可以得到所需的结果:

 StreamWriter utf8Writer = new StreamWriter(proc.StandardInput.BaseStream, Encoding.UTF8); utf8Writer.Write(...); utf8Writer.Close(); 

我刚刚遇到这个问题,无法使用Console.InputEncoding技术,因为它似乎只在控制台应用程序中工作。

因此,我尝试了Victor的答案,但是我遇到了与评论员MvanGeest相同的问题,其中BOM仍然被添加。 过了一会儿,我发现可以创建一个禁用BOM的UTF8Encoding的新实例,这样就可以阻止BOM被写入。 以下是Victor的示例的修改版本,显示了更改。

 StreamWriter utf8Writer = new StreamWriter(proc.StandardInput.BaseStream, new UTF8Encoding(false)); utf8Writer.Write(...); utf8Writer.Close(); 

希望这能节省一些时间。

另一种解决方案是在创建进程之前设置Console InputEncoding。

 Console.InputEncoding = Encoding.UTF8; 

让它工作现在将我的应用程序输出类型设置为控制台应用程序并设法隐藏控制台窗口出现在窗体之前。 它基本上只能在程序运行,控制台窗口弹出和隐藏时正常工作。

 static class Program { [DllImport("kernel32.dll")] static extern bool AttachConsole(int dwProcessId); private const int ATTACH_PARENT_PROCESS = -1; [DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetConsoleCP( uint wCodePageID ); [DllImport("kernel32.dll", SetLastError = true)] public static extern uint GetConsoleCP(); [DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); ///  /// The main entry point for the application. ///  [STAThread] static void Main() { Console.Title = "Stok"; // redirect console output to parent process; // must be before any calls to Console.WriteLine() AttachConsole(ATTACH_PARENT_PROCESS); System.Console.InputEncoding = Encoding.UTF8; IntPtr hWnd = FindWindow(null, "Stok"); //put your console window caption here if (hWnd != IntPtr.Zero) { //Hide the window ShowWindow(hWnd, 0); // 0 = SW_HIDE } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }