写入文本文件导致不必要的换行符

时间:2017-04-13 07:44:51

标签: vb.net ping

所以我正在ping一个smtp以检查IP是否正常工作,然后用我的发现写一个文本文件,一切都是一件事。当它写入文本文件时,它会加载和加载换行符,这是我不想要/不需要的。

                    IO.File.AppendAllText(SummaryfilePath, Now.ToLongTimeString & " - " & URL + vbNewLine)

                    Dim request As SmtpClient = New SmtpClient(URL)
                    Dim address As String()

                    address = request.Host.Split(":")
                    request.Host = address(1)
                    request.Host = request.Host.TrimStart("/")

                    Dim tcpClient As New System.Net.Sockets.TcpClient()

                    tcpClient.Connect(request.Host, request.Port)
                    Try
                        Dim networkStream As Sockets.NetworkStream = tcpClient.GetStream()
                        If networkStream.CanWrite And networkStream.CanRead Then
                            ' Do a simple write.
                            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
                            networkStream.Write(sendBytes, 0, sendBytes.Length)
                            ' Read the NetworkStream into a byte buffer.
                            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                            ' Output the data received from the host to the console.
                            Dim returndata As String = Encoding.ASCII.GetString(bytes)

                            IO.File.AppendAllText(SummaryfilePath, Now.ToLongTimeString & " - " & returndata & vbNewLine & vbNewLine)

                            'Using sw As StreamWriter = File.AppendText(SummaryfilePath)
                            '    sw.WriteLine(Now.ToLongTimeString & " - " & returndata & vbNewLine & vbNewLine)
                            '    sw.WriteLine("")
                            'End Using

                        Else
                            If Not networkStream.CanRead Then
                                Console.WriteLine("cannot not write data to this stream")
                                tcpClient.Close()
                            Else
                                If Not networkStream.CanWrite Then
                                    Console.WriteLine("cannot read data from this stream")
                                    tcpClient.Close()
                                End If
                            End If
                        End If
                    Catch ex As Exception
                        IO.File.AppendAllText(SummaryfilePath, Now.ToLongTimeString & " - ERROR! " & ex.Message + vbNewLine & vbNewLine)
                    End Try

                End If

这是写入文本文件的代码行,之后在执行下一次ping之前在文本文件中至少执行50/60个换行符。

                            IO.File.AppendAllText(SummaryfilePath, Now.ToLongTimeString & " - " & returndata & vbNewLine & vbNewLine)

我也尝试过使用一个编写器,但它也做了同样的事情。我已经看过了返回数据,看看它是否会带来数以百万计的类似休息,但事实并非如此。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

如何使用更简单的东西?

If My.Computer.Network.Ping("ftp://ftp.drivehq.com/") Then
     Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://ftp.drivehq.com/file.txt"), System.Net.FtpWebRequest)
     request.Credentials = New System.Net.NetworkCredential("user", "password")
     request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

     Dim file() As Byte = System.IO.File.ReadAllBytes("c:\file.txt")

     Dim strz As System.IO.Stream = request.GetRequestStream()
     strz.Write(file, 0, file.Length)
     strz.Close()
     strz.Dispose()
Else
     MsgBox("Can't connect to FTP server")
End IF

然后只需将数据写入 c:\ file.txt 当然,建议不要使用 C:\ drive <的基础/ p>