加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB快速文件传输工具

发布时间:2020-12-16 23:34:41 所属栏目:大数据 来源:网络整理
导读:这几天,忙的用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.Sele

这几天,忙的用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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读