2014-11-05 2 views
2

나는 텍스트에서 특정 프로세스 만 나가는 연결을 저장하는 wan't텍스트 파일 vb.net에 multilined 결과를 쓰지 못했습니다

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 

모든 였는지를 위해 모든 TCP 연결을 반환이 기능이 파일 그래서 나는

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

파일 한 줄만 찾습니다. 텍스트 파일에 전체 결과 (외부 연결이있는 각 프로세스)를 작성하는 방법은 무엇입니까?

+0

'Environment.NewLine'을 사용 하시겠습니까? – Baby

+0

각 루프에 대해 새 스트림을 엽니 다. 당신은 SW에 추가하도록 말하지 않기 때문에, 마지막 항목을 그냥 남겨두고 이전 항목을 덮어 쓰고있을 것입니다. MsgBox는 어떤 항목이 루프에서 활성화되어 있는지 보여주는 것입니다. 내게 도움이되는 – Plutonix

답변

0

텍스트 파일에 추가를 설정해야합니다.

당신은 변경해야
에서는 StreamWriter = 새에서는 StreamWriter으로 StreamWriter는 = 새에서는 StreamWriter (의 myFile)
으로
사용 자상 (의 myFile, 참)

으로 SW를 사용하여 사용자가 설정 참을 설정하여 true에 파일을 첨부하십시오.

+0

감사합니다. –

관련 문제