Process.StandardInput または C# コードから実行されたアプリケーションのエンコードの問題 質問する

Process.StandardInput または C# コードから実行されたアプリケーションのエンコードの問題 質問する

エンコードのエンコードに問題がありますProcess.StandartInput。Windows フォーム アプリケーションでプロセスを使用していますが、入力は UTF-8 である必要があります。Process.StandardInput.Encoding読み取り専用なので、UTF-8 に設定できず、Windows のデフォルト エンコードが取得され、UTF-8 で問題のないネイティブ文字が劣化します。プログラムでは 2 つのプロセスが使用されています。1 つはファイルに出力を書き込み、もう 1 つは読み取りです。出力エンコードを 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();

// where encoding should be Encoding.UTF8;
MessageBox.Show(p1.StandardInput.Encoding.EncodingName); 

p1.StandardInput.WriteLine(res);
p1.Close(); 

ベストアンサー1

次の方法で作成された StreamWriter を (StandardInput の代わりに) 使用すると、目的の結果が得られます。

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

おすすめ記事