VB快速文件传输工具

前端之家收集整理的这篇文章主要介绍了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

很方便的传输工具一拖就可以传到别人电脑上去了

猜你在找的VB相关文章