0

我正在使用下面的代码来启动一个新进程并使用 winrar 归档文件:

Private Function RunCmd(ParamArray commands As String()) As String
    Dim returnvalue As String = String.Empty

    Dim info As New ProcessStartInfo("cmd")
    info.UseShellExecute = False
    info.RedirectStandardInput = True
    info.RedirectStandardOutput = True
    info.CreateNoWindow = True



    Using process__1 As Process = Process.Start(info)
        Using sw As StreamWriter = process__1.StandardInput
            Using sr As StreamReader = process__1.StandardOutput
                For Each command As String In commands
                    sw.WriteLine(command)
                    IO.File.AppendAllText(workingDir & "\log.txt", command & vbCrLf)
                Next
                sw.Close()
                returnvalue = sr.ReadToEnd()
                sr.Close()
            End Using
        End Using
    End Using

    info = Nothing


    Return returnvalue
End Function

此代码无法归档名称中包含 Unicode 字符的文件。我得到的是:警告:没有文件

如果我在命令提示符下手动运行相同的命令,一切正常。将命令输出到文件的行正确输出命令(存在 unicode 字符)传递给此函数的命令示例如下:

rar.exe u "\\mypath\myRarFile.rar" -m5 -wE:\WorkingDir "\\pathToFile\miljö.txt" 

谢谢,杰森

4

1 回答 1

0

这解决了我的问题:

Private Function RunCmd(ParamArray commands As String()) As String
    Dim returnvalue As String = String.Empty

    Dim info As New ProcessStartInfo("cmd")
    info.UseShellExecute = False
    info.RedirectStandardInput = True
    info.RedirectStandardOutput = True
    info.CreateNoWindow = True

    Using process__1 As Process = Process.Start(info)
        Using sw As StreamWriter = process__1.StandardInput
            Using sr As StreamReader = process__1.StandardOutput
                For Each command As String In commands

                    sw.WriteLine("chcp 65001")

                    sw.WriteLine(command)
                Next
                sw.Close()
                returnvalue = sr.ReadToEnd()
                sr.Close()
            End Using
        End Using
    End Using
    info = Nothing

    Return returnvalue
End Function
于 2013-01-15T15:13:24.973 回答