代码如下:Dim TmpFile As String, TmpStr As String
    Dim StrIndex As Integer
    Dim cmdstr
    Dim count As String
    Dim sFile As Variant
    IsLinkNet = False
    TmpFile = App.Path & "\ip.txt"
    MsgBox TmpFile
    If Dir(TmpFile) <> "" Then Kill TmpFile
    cmdstr = "cmd /c ping " & IP & ">" & TmpFile
    Shell cmdstr, vbHide
        Open TmpFile For Input As #1
    Do Until EOF(1)

解决方案 »

  1.   

    ip.txt和你的app.exe在同一文件夹吗?
    如果不在,要保证他们是在同一文件夹下
      

  2.   

    'F5执行很快,shell内部的ping命令还没有执行完,你就open ip.txt文件了
    '而按F8单步执行的时候,有时间停留,所以shell命令已经执行完毕。Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Dim StrIndex As Integer
    Dim cmdstr
    Dim count As String
    Dim sFile As Variant
    TmpFile = App.Path & "\ip.txt"
    If Dir(TmpFile) <> "" Then Kill TmpFile
    cmdstr = "cmd /c ping " & IP & ">" & TmpFile
    Shell cmdstr, vbHidesleep 1000 <-------------- '让程序在此停止1秒钟,等shell命令执行完Open TmpFile For Input As #1    
    Do Until EOF(1)
       .....
      

  3.   

    加延时或者等待 COMMAND 命令的返回(这个要用 API)
      

  4.   

    后续代码比磁盘操作跑得快!
    这样:
    do
    If Dir(TmpFile) <> "" Then Kill TmpFile
    loop until dir(Tmpfile)=""
      

  5.   

    或者:
    If Dir(TmpFile) <> "" Then Kill TmpFile
    do
    loop until dir(Tmpfile)=""
      

  6.   

    调用API OpenProcess 和 WaitForSingleObject
    其中 openporcess返回进程句柄,参数由shell返回的id指定
    waitforsingleobject传递进程句柄做参数, 程序将组塞到这里直到shell执行完毕