【问题标题】:Unicode characters in processes and winrar进程和winrar中的Unicode字符
【发布时间】:2013-01-14 13:39:43
【问题描述】:

我正在使用下面的代码来启动一个新进程并使用 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" 

谢谢, 杰森

【问题讨论】:

  • Eugene Roshal 是一个优秀的程序员,但 Unicode 从来都不是他的强项。使用库或升级到 7-zip。

标签: .net unicode winrar


【解决方案1】:

这解决了我的问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多