0

我正在 VB.NET 2.0 中编写代码,并希望使用 FTP -s:filename 命令为 FTP 运行批处理命令。

我有一个用于 FTP 上传的批处理文件 FTP.TXT。它有以下语句:

OPEN <FPT SERVER IP>
USERNAME
PASSWORD
ASC
CD FOLDERNAME
PUT D:\DRFT000009.TXT FTPDRFTIN.DRFT000009
BYE

我必须动态更改批处理文件中的文件名。现在要么我可以在运行时创建一个批处理文件然后读取它,要么我得到一个代码来设置 Process 对象的输入流。但它没有按预期工作。

这段代码运行良好,但在这里我从计算机上读取了一个静态批处理文件 FTP.TXT:

Public Sub FTP4()
    Dim psi As ProcessStartInfo
    Dim totalerror As String = ""
        psi = New ProcessStartInfo()
        psi.FileName = "FTP.EXE"
        psi.Arguments = " -s:D:\FTP.TXT"
        psi.RedirectStandardError = True
        psi.RedirectStandardOutput = True
        psi.CreateNoWindow = True
        psi.WindowStyle = ProcessWindowStyle.Hidden
        psi.UseShellExecute = False

        Dim process As Process = process.Start(psi)
        Dim error2 As String = process.StandardError.ReadToEnd()
        totalerror = totalerror & error2
        process.WaitForExit()


        Response.Write(totalerror)

End Sub

但我想以某种方式为每个请求使用自定义文件名完成 FTP。这是我尝试过但不起作用的方法:

Public Sub FTP5()
    Dim totalerror As String = ""
    Dim BatchScriptLines(6) As String

    Dim process As New Process
    process.StartInfo.FileName = "FTP.EXE"
    process.StartInfo.UseShellExecute = False
    process.StartInfo.CreateNoWindow = True
    process.StartInfo.RedirectStandardInput = True
    process.StartInfo.RedirectStandardOutput = True
    process.StartInfo.RedirectStandardError = True
    process.Start()
    process.BeginOutputReadLine()
    Using InputStream As System.IO.StreamWriter = process.StandardInput
        InputStream.AutoFlush = True
        BatchScriptLines(0) = "OPEN <FPT IP ADDRESS>"
        BatchScriptLines(1) = "USERNAME"
        BatchScriptLines(2) = "PASSWORD"
        BatchScriptLines(3) = "ASC"
        BatchScriptLines(4) = "CD SFCD40DAT"
        BatchScriptLines(5) = "PUT D:\DRFT000006.TXT FTPDRFTIN.DRFT000006"
        BatchScriptLines(6) = "BYE"
        For Each ScriptLine As String In BatchScriptLines
            InputStream.Write(ScriptLine & vbCrLf)
        Next
    End Using

    Dim error2 As String = process.StandardError.ReadToEnd()
    totalerror = totalerror & error2
    process.WaitForExit()


    Response.Write(totalerror)

End Sub

请告知我如何在这种情况下执行“FTP -s:filename”命令。基本上我想做一些类似于我不能做的单行批处理文件执行的事情。

4

1 回答 1

1

作为具有清晰格式的简单文本文件,您可以重写文件并传递需要动态更改的参数

Public Sub FTP4()

    ' Of course I assume that, at this point your program knows the exact values '
    ' to pass at the procedure that rebuilds the file'
    PrepareFTPFile("D:\DRFT000006.TXT", "USERNAME", "PASSWORD")

    ' Now you call the original code.....
    Dim psi As ProcessStartInfo
    Dim totalerror As String = ""
    psi = New ProcessStartInfo()
    psi.FileName = "FTP.EXE"
    psi.Arguments = " -s:D:\FTP.TXT"
    ....
End Sub


Public Sub PrepareFTPFile(fileToUpload as String, username as string, userpass as string)
    Using sw = new StreamWriter("D:\FTP.TXT", False)
       sw.WriteLine("OPEN <FPT IP ADDRESS>")
       sw.WriteLine(username)
       sw.WriteLine(userpass)
       sw.WriteLine("ASC")
       sw.WriteLine("CD SFCD40DAT")
       sw.WriteLine("PUT " + fileToUpload + " FTPDRFTIN." + Path.GetFileNameWithoutExtension(fileToUpdload))
       sw.WriteLine("BYE")
    End Using
End Sub
于 2013-06-17T15:40:44.780 回答