process.standardInput编码问题

Mus*_*afa 16 c# encoding process utf-8

我对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(); 
Run Code Online (Sandbox Code Playgroud)

小智 24

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

StreamWriter utf8Writer = new StreamWriter(proc.StandardInput.BaseStream, Encoding.UTF8);
utf8Writer.Write(...);
utf8Writer.Close();
Run Code Online (Sandbox Code Playgroud)


小智 8

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

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

StreamWriter utf8Writer = new StreamWriter(proc.StandardInput.BaseStream, new UTF8Encoding(false));
utf8Writer.Write(...);
utf8Writer.Close();
Run Code Online (Sandbox Code Playgroud)

希望这能节省一些时间.


Nic*_*ell 5

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

Console.InputEncoding = Encoding.UTF8;
Run Code Online (Sandbox Code Playgroud)


Mus*_*afa 2

现在让它工作,将我的应用程序输出类型设置为控制台应用程序,并设法隐藏出现在表单之前的控制台窗口。仅当程序运行时,它基本上才像正常情况一样工作,控制台窗口会弹出并隐藏。

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);

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [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());
    }
}
Run Code Online (Sandbox Code Playgroud)