Vb.net VB FTP代码在调试(F10)中工作,但在运行或F5时不工作

Vb.net VB FTP代码在调试(F10)中工作,但在运行或F5时不工作,vb.net,visual-studio,shell,ftp,Vb.net,Visual Studio,Shell,Ftp,我需要将一个本地文件FTP到大型机,并编写了以下脚本,以使用streamwriter创建一个本地批处理文本文件,然后将该文件与FTP-s:命令一起使用以运行它 这是代码 Shared Sub TestFTP() ' BP DEFINED INPUTS ANDF OUTPUTS 'Inputs Dim hostname As String Dim username As String Dim password As String Dim ma

我需要将一个本地文件FTP到大型机,并编写了以下脚本,以使用streamwriter创建一个本地批处理文本文件,然后将该文件与FTP-s:命令一起使用以运行它

这是代码

 Shared Sub TestFTP()

    '  BP DEFINED INPUTS ANDF OUTPUTS

    'Inputs
    Dim hostname As String
    Dim username As String
    Dim password As String
    Dim mainfile As String
    Dim localfile As String

    'Outputs
    Dim success As Boolean
    Dim message As String

    '-------------
    'Test DATA
    '-------------

    hostname = "XXIBM2"
    username = "USER1"
    password = "XXX1234"
    mainfile = "XXTSO.USER1.TEST2"
    localfile = "D:\TestFTP.txt"

    '=============================BP Code========================
    Try
        Dim localPath As String = "C:\BPFTP"
        Dim isExists As Boolean = System.IO.Directory.Exists(localPath)

        If (isExists = False) Then
            System.IO.Directory.CreateDirectory(localPath)
        End If

        ' Open StreamWriter And create batch file

        Using writer As StreamWriter = New StreamWriter(localPath + "\\FTP.txt")

            writer.WriteLine("open " + hostname)
            writer.WriteLine(username)
            writer.WriteLine(password)
            writer.WriteLine("put " + localfile + " '" + mainfile + "'")
            writer.WriteLine("bye")
            writer.WriteLine("exit")
        End Using

        ' Perform FTP

        Interaction.Shell("ftp -n -s:C:\BPFTP\FTP.txt") 

        ' Delete batch file

        System.IO.File.Delete("C:\\BPFTP\\FTP.txt")
        success = True

    Catch e As Exception

        success = False
        message = e.Message

    End Try


End Sub
如果我使用F5运行代码,该文件不会出现在大型机上

如果我在Shell命令中设置断点,并将代码(f5)运行到此处,然后再运行f5到末尾,则该文件不会将FTPd发送到大型机

不过


如果我将代码运行到断点,然后使用F10简单地“跨过”Shell命令行,那么文件将成功地FTPs到大型机。

当您在调试模式下运行时,您将强制执行同步操作,您需要告诉Shell和您的ftp应用程序等待以完全发送文件

Interaction.Shell("ftp -n -s:C:\BPFTP\FTP.txt", AppWinStyle.MinimizedFocus, True, 30000)


这将迫使它等待30秒再继续,如果将其设置为-1,它将永远等待,这可能会导致不良行为。

谢谢。除此之外,超时是以毫秒为单位的,而不是以秒为单位的,所以我必须将其更改为1000,以给我1秒的时间。那很好。更正了我的答案。你的问题是一个棘手的问题,我有自己与其他进程,我不得不壳。不知道你的意思,但我有管理权。吉米·史密斯为下面的问题提供了解决方案。