传递给Process.Start的参数中的字符串编码错误

时间:2017-11-29 13:13:03

标签: vb.net character-encoding arguments

我正在使用cpdf来连接PDF文件。

这是由以下代码完成的:

Private Shared Sub ConcatenateWithCpdf(
                                      outputFileName As String,
                                      inputfilenames() As String,
                                      Optional timeout As Integer = 15000,
                                      Optional ByRef process_exitcode As Integer = 0,
                                      Optional ByRef process_output As String = Nothing,
                                      Optional ByRef process_erroroutput As String = Nothing)
    Dim output As New Text.StringBuilder()
    Dim erroroutput As New Text.StringBuilder()
    Using process As New Process()
        process.StartInfo.FileName = CpdfPath()
        process.StartInfo.Arguments = String.Format(
            "{0} -o ""{1}""",
            Join(
                inputfilenames.Select(
                    Function(s) String.Format("""{0}""", s)).ToArray,
                " "),
            outputFileName)
        process.StartInfo.CreateNoWindow = True
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.RedirectStandardError = True
        Using outputWaitHandle As New Threading.AutoResetEvent(False)
            Using errorWaitHandle As New Threading.AutoResetEvent(False)
                AddHandler process.OutputDataReceived,
                    Sub(sender As Object, e As DataReceivedEventArgs)
                        If e.Data Is Nothing Then
                            outputWaitHandle.Set()
                        Else
                            output.AppendLine(e.Data)
                        End If
                    End Sub
                AddHandler process.ErrorDataReceived,
                    Sub(sender As Object, e As DataReceivedEventArgs)
                        If e.Data Is Nothing Then
                            errorWaitHandle.Set()
                        Else
                            erroroutput.AppendLine(e.Data)
                        End If
                    End Sub
                process.Start()
                process.BeginOutputReadLine()
                process.BeginErrorReadLine()
                If process.WaitForExit(timeout) AndAlso
                    outputWaitHandle.WaitOne(timeout) AndAlso
                    errorWaitHandle.WaitOne(timeout) Then
                    process_exitcode = process.ExitCode
                End If
            End Using
        End Using
    End Using
    process_output = output.ToString
    process_erroroutput = erroroutput.ToString
End Sub

我的问题是,某些输入文件名具有非ASCII字符,如下所示:

C:\Users\myuser\AppData\Local\Temp\Procuração - Processo 5001092-92.2017.4.03.6114.pdf

在这种情况下,CPDF失败并返回此标准输出错误文本:

  

cpdf遇到错误。技术细节如下:

     

C:\ Users \ myuser \ AppData \ Local \ Temp \Procurac¸a~o - Processo 5001092-92.2017.4.03.6114.pdf:没有这样的文件或目录

显然,在传递参数时,文件名被某种编码不匹配所破坏。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可能需要使用以下代码检查文件:

process.StartInfo.FileName = CpdfPath()。Normalize(NormalizationForm.FormD)

看看哪种NormalizationForm最适合你:

<强> FormC

表示使用完整的规范分解对Unicode字符串进行规范化,然后在可能的情况下用其主要复合替换序列。

<强> FormD

表示使用完整的规范分解对Unicode字符串进行规范化。

<强> FormKC

表示使用完全兼容性分解对Unicode字符串进行规范化,然后在可能的情况下用其主要复合替换序列。

FormKD

表示使用完全兼容性分解对Unicode字符串进行规范化。