【问题标题】:VB FTP code works in Debug (F10) but not when run or F5VB FTP 代码在调试 (F10) 中有效,但在运行或 F5 时无效
【发布时间】:2016-11-23 21:56:33
【问题描述】:

我需要将本地文件通过 FTP 传输到 Mainframe,并编写了以下脚本以使用 streamwriter 创建本地 Batch 文本文件,然后将此文件与 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 到末尾,则文件不会通过 FTP 传输到大型机。

但是。

如果我将代码运行到断点,然后使用 F10 简单地“跳过”Shell 命令行,则文件成功地通过 FTP 传输到大型机。

【问题讨论】:

  • 不确定你的意思,但我有管理员权限。 Jimmy Smith 为以下问题提供了解决方案。

标签: vb.net visual-studio shell ftp


【解决方案1】:

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

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

See here

这将强制它在继续之前等待 30 秒,如果将其设置为 -1,它将永远等待,这可能会导致不良行为。

【讨论】:

  • 谢谢吉米。除了超时时间是毫秒而不是秒之外,这还有效,所以我不得不将它更改为 1000 给我 1 秒。那时效果很好。
  • 更正了我的回答。你的问题是一个棘手的问题,我在其他进程中遇到了我不得不外壳的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 1970-01-01
  • 2021-08-09
相关资源
最近更新 更多