输出文件名中的UTF-8字符时pg_dump失败

时间:2015-03-09 19:47:57

标签: c# windows postgresql utf-8 pg-dump

我正在尝试使用pg_dump创建数据库备份。数据库名称具有非ASCII字符,例如本物ではないデータベース名。

我正在拨打以下电话:

pg_dump.exe --file="C:\Users\AUser\AppData\Local\Temp\本物ではないデータベース名_20150309185239.pgbak" pg_backupc4c978f4aebc4153adcabab6e097c347

然而,它失败并出现以下错误:

pg_dump: [archiver] could not open output file "C:\Users\AUser\AppData\Local\Temp\????????????_20150309185239.pgbak": Invalid argument

我不明白的是,其他命令适用于非ASCII字符。例如,mdkdir 本物ではないデータベース名工作正常。

这是pg_dump中的错误还是我做错了什么?

更新

根据@Mike Sherrill' Cat Recall'在评论中,我测试了重定向,它在Powershell和标准命令行中都有效。

然而,我尝试使用C#自动执行此操作,当我尝试使用C#Process运行上述命令时,它失败了:

        ProcessStartInfo psi = new ProcessStartInfo(command, args);
        psi.CreateNoWindow = true;
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = psi.RedirectStandardError = true;
        using (Process process = new Process())
        {
            process.StartInfo = psi;
            string s = "";
            process.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e)
            {
                s += e.Data;
            };
            string s1 = "";
            process.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
            {
                s1 += e.Data;
            };
            process.Start();
            process.BeginErrorReadLine();
            process.BeginOutputReadLine();
            TimeSpan maxTime = TimeSpan.FromMinutes(10);
            if (!process.WaitForExit((int)maxTime.TotalMilliseconds))
            {
                throw new Exception("Command " + command + " " + args + " Timed out after " + maxTime.TotalSeconds);
            }
            if (process.ExitCode != 0)
            {
                throw new Exception("Command " + command + " " + args + " failed with " + s + " " + s1);
            }
        }

同样,我可以制作一个C#Process并使用日文字母调用mkdir,一切正常。

1 个答案:

答案 0 :(得分:1)

Per @ MikeSherrill' CatRecall':

"文件选项--file由pg_dump代码解析和处理,可能无法编写以处理UTF-16。 (源代码)重定向由shell完成。如果你想让它工作,你需要重写部分pg_dump(),或者使用命令行重定向。"