VB快速文件傳輸工具

截圖視屏這幾天,忙的用VB 2005開發拖動文件式傳輸工具,上圖,代碼如下

form1部分代碼:

Imports System.IO
Public Class Form1
    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
        If ListBox1.SelectedIndex >= 0 Then
            Process.Start(Convert.ToString(ListBox1.SelectedItem))
        End If
    End Sub
    Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
        Dim files() As String
        files = e.Data.GetData(DataFormats.FileDrop)
        Dim file As String
        For Each file In files
            ListBox1.Items.Add(file)
        Next
    End Sub
    Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String
        s = ListBox1.SelectedItem
        ListBox1.Items.Remove(s)
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim s As String
        s = ListBox1.SelectedItem
        Dim fileName As New FileInfo(s)
        Form2.Show()
        Dim LItem As New ListViewItem(fileName.Name)
        LItem.SubItems.Add(fileName.DirectoryName)
        LItem.SubItems.Add(fileName.Length & " " & "KB")
        Form2.ListView1.Items.Add(LItem)
    End Sub
End Class

 

form2部分代碼:

Imports System.Net
Imports System.Threading
Imports System.Net.Sockets
Imports System.IO
Public Class Form2
    Private readThread As Thread
    Private connection As Socket
    Private socketStream As NetworkStream
    Private writer As BinaryWriter
    Private reader As BinaryReader
    Dim Item As String
    Dim s As String
    Dim i As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        readThread = New Thread(New ThreadStart(AddressOf RunServer))
        readThread.Start()
        s = Me.ListView1.SelectedItems.ToString()
        Dim BR As BinaryReader
        Dim FS As FileStream
        FS = New FileStream(s, FileMode.Open)
        BR = New BinaryReader(FS)
        Dim itm As Byte
        Item = BR.ReadByte()
        While Not Item = Nothing
            writer.Write(itm)
            itm = BR.ReadByte()
        End While
    End Sub
    Public Sub RunServer()
        Dim listener As TcpListener
        Try
            Dim IP As IPAddress = IPAddress.Parse(Me.TextBox1.Text)
            listener = New TcpListener(IP, 50000)
            listener.Start()
            Me.Label3.Text = "正在連接..."
            Me.Name = "正在傳送文件:" & s
            Dim Reply As Byte
            While True
                connection = listener.AcceptSocket()
                socketStream = New NetworkStream(connection)
                writer = New BinaryWriter(socketStream)
                reader = New BinaryReader(socketStream)
                Me.Label3.Text = "連接成功"
                Do
                    Try
                        Reply = reader.ReadByte()
                    Catch ex As Exception

                    End Try
                Loop While connection.Connected
            End While
            writer.Close()
            reader.Close()
            socketStream.Close()
            connection.Close()
        Catch ex As Exception
        End Try
    End Sub
    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        s = Me.ListView1.SelectedItems.ToString()
        Me.Name = "將要傳送文件是:" & s
    End Sub
End Class

 

很方便的傳輸工具一拖就可以傳到別人電腦上去了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章