2

看来我对 ASP.Net VB.Net 中的反斜杠并不幸运。

我正在尝试使用 ffprobe 输出有关文件的一些信息,并且我的路径在每个包含反斜杠的字符串中的某个反斜杠处随机剪切。

我调试了这个函数来返回fileToProcess生成的路径:

Function ffprobe_show_format(ByVal arg As String) As String
    Dim myProcess As New System.Diagnostics.Process()
    Dim fileToProcess As String = MapPath(".") & "\temps\" & arg
    myProcess.StartInfo.FileName = MapPath(".") & "\ffprobe.exe"
    myProcess.StartInfo.Arguments = "-show_format " & fileToProcess & " >C:\inetpub\vhosts\mysite.com\httpdocs\absolutefs\ffmpegtemp.txt"
    myProcess.StartInfo.UseShellExecute = False
    myProcess.StartInfo.RedirectStandardInput = True
    myProcess.StartInfo.RedirectStandardOutput = True
    myProcess.Start()
    Dim myStreamWriter As StreamWriter = myProcess.StandardInput
    Dim mystreamreader As StreamReader = myProcess.StandardOutput
    Dim str As String = mystreamreader.ReadToEnd
    Return str & ";" & "FileToProcess=" & fileToProcess & "MapPath(.) AND ffprobe.exe" & MapPath(".") & "\\ffprobe.exe"
End Function

它返回;FileToProcess=C:

代表着

  • 由于某些错误,我的文件未处理
  • 我的路径在反斜杠处被切断
  • 然后将其余的字符串断开

我是否需要告诉 asp.net 以另一种方式表示反斜杠?

[编辑]

我无法选择答案,但会投赞成票,因为我犯了两个错误: - 为了调试,我正在读取我的值,一旦它被放入具有限制大小的 SQL 变量然后 - ...使用可能拒绝的 Newtonsoft.Json 解析器获取一些字符。

我是 ASP.Net 的新手,所以我很难找到调试的好方法。因此,当我无法在平台上进行调试时,我终于像往常一样:我已经在文本文件中编写了调试变量,并且所有内容都使用这种方式:

Function ffprobe_show_format(ByVal file As String, ByVal app As String) As String        
    Dim myProcess As New System.Diagnostics.Process()
    myProcess.StartInfo.FileName = app
    Dim argz As String = FormatCommandLine("-show_format", file)
    myProcess.StartInfo.Arguments = argz
    myProcess.StartInfo.UseShellExecute = False
    myProcess.StartInfo.RedirectStandardOutput = True
    myProcess.Start()
    Dim mystreamreader As StreamReader = myProcess.StandardOutput str As String = mystreamreader.ReadToEnd
    MapPath(".") & "/ffprobe.exe"
    Dim objWriter As New System.IO.StreamWriter(MapPath(".") & "\debug.txt")
    objWriter.Write(str)
    objWriter.Close()
    Return str
End Function

我将添加一个 ffmpeg 标签,因为它也是如何调用它和处理文件的另一个示例。

4

4 回答 4

5

您无需担心反斜杠。反斜杠在 VB 中不像在 C# 中那样是特殊字符。事实上,如果你把它们加倍,你可能会遇到错误。

很难说问题出在哪里。尝试缩小范围的一些想法:

  • 尝试不要重定向 StandardInput,因为您没有传递任何东西。

  • 从 StandardError 读取以确保它为空。它可能包含错误消息。

  • 我会使用 Debug.WriteLine 来跟踪函数并尝试找出问题开始的地方。

  • 这可能无关紧要,但我会将 myProcess.WaitForExit 放在 mystreamreader.ReadToEnd 之后。

  • Larry 建议使用 System.IO.Path 是一个很好的建议。

一些基于所有这些仓促修改的代码:

Function ffprobe_show_format(ByVal arg As String) As String

    Debug.WriteLine("arg: " & arg)

    Dim fileToProcess As String = IO.Path.Combine(MapPath("."), "temps\" & arg)
    Debug.WriteLine("fileToProcess = " & fileToProcess)

    Debug.WriteLine("MapPath AND ffprobe.exe: " & IO.Path.Combine(MapPath(".") & "\\ffprobe.exe"))

    Dim myProcess As New System.Diagnostics.Process()
    myProcess.StartInfo.FileName = IO.Path.Combine(MapPath("."), "\ffprobe.exe")
    myProcess.StartInfo.Arguments = "-show_format " & fileToProcess & " >C:\inetpub\vhosts\mysite.com\httpdocs\absolutefs\ffmpegtemp.txt"
    myProcess.StartInfo.UseShellExecute = False
    'myProcess.StartInfo.RedirectStandardOutput = True
    myProcess.StartInfo.RedirectStandardError = True
    myProcess.Start()
    'Dim str As String = myProcess.StandardOutput.ReadToEnd
    Dim errStr as string = myProcess.StandardError.ReadToEnd
    myProcess.WaitForExit()

    Debug.WriteLine("myProcess exit code = " & myProcess.ExitCode.ToString())
    'Debug.WriteLine("stdOutput: " & str)
    Debug.WriteLine("stdError: " & errStr)

    Return str & ";" & "FileToProcess=" & fileToProcess

End Function
于 2012-06-18T18:18:08.207 回答
3

这里有几件事需要考虑。

首先,反斜杠字符是转义字符(即 \t \n \r ...),如果您必须将其嵌入字符串中,则应该双转义 \\。

但是,.NET 框架包含一个非常好的类:System.IO.Path,它具有多个 Combine 方法重载,可以帮助您构建路径,而无需将反斜杠字符嵌入到字符串中。这也使您的代码在您将在非 Windows 平台上运行它的(有点不太可能)事件中更具可移植性。

http://msdn.microsoft.com/en-us/library/system.io.path

于 2012-06-18T17:18:21.280 回答
2

StreamReader在访问其内容之前尝试关闭:

Dim myStreamReader As StreamReader = myProcess.StandardOutput
Dim str As String = myStreamReader.ReadToEnd
myStreamReader.Close()
Return str & ";" & "FileToProcess=" & fileToProcess & "MapPath(.) AND ffprobe.exe" & MapPath(".") & "\\ffprobe.exe"

当我在本地 devbox 中运行您的功能时:

Function ffprobe_show_format(ByVal arg As String) As String
    Dim myProcess As New System.Diagnostics.Process()
    Dim fileToProcess As String = HttpContext.Current.Server.MapPath(".") & "\temps\" & arg
    myProcess.StartInfo.FileName = HttpContext.Current.Server.MapPath(".") & "\ffprobe.exe"
    myProcess.StartInfo.Arguments = "-show_format " & fileToProcess & " >C:\inetpub\vhosts\mysite.com\httpdocs\absolutefs\ffmpegtemp.txt"
    'myProcess.StartInfo.UseShellExecute = False
    'myProcess.StartInfo.RedirectStandardInput = True
    'myProcess.StartInfo.RedirectStandardOutput = True
    'myProcess.Start()
    'Dim myStreamWriter As StreamWriter = myProcess.StandardInput
    'Dim myStreamReader As StreamReader = myProcess.StandardOutput
    Dim str As String = String.Empty 'myStreamReader.ReadToEnd
    Return str & ";" & "FileToProcess=" & fileToProcess & "MapPath(.) AND ffprobe.exe" & HttpContext.Current.Server.MapPath(".") & "\\ffprobe.exe"
End Function

我得到了一个我认为是正常返回的字符串ffprobe_show_format

";FileToProcess=C:\Documents and Settings\Me\My Documents\Proj1\SubApp1\temps\testMapPath(.) AND ffprobe.exeC:\Documents and Settings\Me\My Documents\Proj1\SubApp1\\"

我注释掉了,System.Diagnostics.Process()因为我没有一个名为ffprobe.exe.

鉴于它的其余部分有效,请尝试在访问其内容之前关闭 StreamReader。

于 2012-06-18T18:42:57.060 回答
1

您应该只需要用另一个反斜杠来转义每个反斜杠。
或者,您可以在字符串前面加上 @ 符号(在 C# 中工作,不确定 VB.NET)。

IE
使用 "C:\this\that.exe"
或 @"C:\this\that.exe" 代替 "C:\this\that.exe"

于 2012-06-18T17:16:19.170 回答