无法将多行显示的结果写入文本文件vb.net

时间:2014-11-05 23:44:17

标签: vb.net multiline

我有这个函数返回所有进程的所有TCP连接

Declare Auto Function GetExtendedTcpTable Lib "iphlpapi.dll" (ByVal pTCPTable As IntPtr, ByRef OutLen As Integer, ByVal Sort As Boolean, ByVal IpVersion As Integer, ByVal dwClass As Integer, ByVal Reserved As Integer) As Integer
Const TCP_TABLE_OWNER_PID_ALL As Integer = 5
<StructLayout(LayoutKind.Sequential)> _
Public Structure MIB_TCPTABLE_OWNER_PID
    Public NumberOfEntries As Integer 'number of rows
    Public Table As IntPtr 'array of tables
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure MIB_TCPROW_OWNER_PID
    Public state As Integer 'state of the connection
    Public localAddress As UInteger
    Public LocalPort As Integer
    Public RemoteAddress As UInteger
    Public remotePort As Integer
    Public PID As Integer 'Process ID
End Structure
Structure TcpConnection
    Public State As TcpState
    Public localAddress As String
    Public LocalPort As Integer
    Public RemoteAddress As String
    Public remotePort As Integer
    Public Proc As String
End Structure
Function GetAllTCPConnections() As MIB_TCPROW_OWNER_PID()
    GetAllTCPConnections = Nothing
    Dim cb As Integer
    GetExtendedTcpTable(Nothing, cb, False, 2, TCP_TABLE_OWNER_PID_ALL, 0)
    Dim tcptable As IntPtr = Marshal.AllocHGlobal(cb)
    If GetExtendedTcpTable(tcptable, cb, False, 2, TCP_TABLE_OWNER_PID_ALL, 0) = 0 Then
        Dim tab As MIB_TCPTABLE_OWNER_PID = Marshal.PtrToStructure(tcptable, GetType(MIB_TCPTABLE_OWNER_PID))
        Dim Mibs(tab.NumberOfEntries - 1) As MIB_TCPROW_OWNER_PID
        Dim row As IntPtr
        For i As Integer = 0 To tab.NumberOfEntries - 1
            row = New IntPtr(tcptable.ToInt32 + Marshal.SizeOf(tab.NumberOfEntries) + Marshal.SizeOf(GetType(MIB_TCPROW_OWNER_PID)) * i)
            Mibs(i) = Marshal.PtrToStructure(row, GetType(MIB_TCPROW_OWNER_PID))
        Next
        GetAllTCPConnections = Mibs
    End If
    Marshal.FreeHGlobal(tcptable)
End Function
Function MIB_ROW_To_TCP(ByVal row As MIB_TCPROW_OWNER_PID) As TcpConnection
    Dim tcp As New TcpConnection
    tcp.State = DirectCast(row.state, TcpState) 'a State enum is better than an int
    Dim ipad As New IPAddress(row.localAddress)
    tcp.localAddress = ipad.ToString
    tcp.LocalPort = row.LocalPort / 256 + (row.LocalPort Mod 256) * 256
    ipad = New IPAddress(row.RemoteAddress)
    tcp.RemoteAddress = ipad.ToString
    tcp.remotePort = row.remotePort / 256 + (row.remotePort Mod 256) * 256
    Dim p As Process = Process.GetProcessById(row.PID)
    tcp.Proc = p.ProcessName
    p.Dispose()
    Return tcp
End Function

我不想仅存储文本文件中某些进程的外向连接,所以我使用了

Sub main()
    For Each Row In GetAllTCPConnections()
        Dim Tcp As TcpConnection = MIB_ROW_To_TCP(Row)
        Dim RemoteAddress As String = Tcp.RemoteAddress.ToString
        Dim process As String = Tcp.Proc
        If (process = "chrome" Or process = "Viber" Or process = "ddns") And (RemoteAddress <> "127.0.0.1") And (RemoteAddress <> "0.0.0.0") Then
            Dim myFile As String = "C:\TCP.txt"
            Using sw As StreamWriter = New StreamWriter(myFile)
                Dim line As String = Tcp.RemoteAddress & "|" & Tcp.localAddress & "|" & Tcp.LocalPort & "|" & Tcp.Proc
                    sw.WriteLine(line)
                    MsgBox(line)
            End Using
        End If
    Next

End Sub

msgbox可以很好地显示每个进程以及由它建立的连接,但是当我打开

  

TCP.txt

文件我只找到一行。 那么如何将整个结果(每个进程及其正在进行的连接)写入文本文件?

1 个答案:

答案 0 :(得分:0)

您需要将追加设置为文本文件。

您需要更改:
使用sw As StreamWriter = New StreamWriter(myFile)
为了
使用sw As StreamWriter = New StreamWriter(myFile,True)

通过设置true,您可以将append to file设置为true