VB.NET File Transfer

How to transfer files using VB.NET

Page 1 of 1

4 Replies - 55678 Views - Last Post: 19 July 2009 - 11:48 AM Rate Topic: -----

#1 user22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-April 07

VB.NET File Transfer

Posted 30 April 2007 - 12:17 PM

The link below gives a really simple way to create a chat program. I want to modify it to transfer files as well. I tried reading files using the binary reader and then writing the information into another file, but the most I can accomplish is gibberish or copying the text of one document and putting it in a new document.

http://www.dreaminco...wtopic21431.htm

Private Function ReadFile(ByVal filePath As String)
		Dim fs As IO.FileStream
		Dim br As IO.BinaryReader
		Dim bytes(10) As Byte
		Dim TheFile As String = ""
		Dim cutpath() As String = filePath.Split("\")

		fs = New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
		br = New IO.BinaryReader(fs)

		Dim byteRead As Byte
		Dim x As Integer
		For x = 0 To br.BaseStream.Length() - 1
			byteRead = br.ReadByte
			TheFile += CStr(byteRead)
		Next
		br.Close()
		Return "!Binary!" & TheFile & "*" & cutpath(cutpath.Length - 1)
End Function
Private Sub SaveFile(ByVal TheBinaries As String)
		'TheBinaires have Data*NameOfFile, this will seperate the two.
		Dim TheBin() As String = TheBinaries.Split("*")
		Dim fs As IO.FileStream
		Dim bw As IO.BinaryWriter
		fs = New IO.FileStream("c:\" & "copy_" & TheBin(1), IO.FileMode.CreateNew, IO.FileAccess.Write)
		bw = New IO.BinaryWriter(fs)
		bw.Write(TheBin(0))
		'bw.Write(byteRead)
		bw.Flush()
		fs.Close()
		bw.Close()
End Sub



The "!Binary!" is there to tell my program call SaveFile. It gets pulled out before writing to the file though.

Is This A Good Question/Topic? 0
  • +

Replies To: VB.NET File Transfer

#2 whisla13  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 10
  • Joined: 22-May 07

Re: VB.NET File Transfer

Posted 22 May 2007 - 10:45 AM

Transferring a file can be as simple as reading data from one stream, and writing it to another. Using this code:

Public Sub TransferData(FromStream As IO.Stream, ToStream As IO.Stream, BufferSize As Integer)
	 Dim buffer(BufferSize - 1) As Byte
	 Do While True
		  Dim bytesRead As Integer = FromStream.Read(buffer, 0, buffer.Length)
		  If bytesRead = 0 Then Exit Do
		  ToStream.Write(buffer, 0, bytesRead)
	 Loop
End Sub



For example, to transfer a file through a network using a Socket, you can create a new NetworkStream which incapsulates the socket. Using the code I posted above, pass a FileStream to the FromStream parameter, and pass the NetworkStream to the ToStream parameter. BufferSize can be 1024 (1 KB).

Hope this helps you out.

- Andrew

This post has been edited by whisla13: 22 May 2007 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 Rozie0910  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-October 08

Re: VB.NET File Transfer

Posted 13 October 2008 - 11:19 PM

this is my coding to read from database to specific place..
i dont know how to make this coding read from other destination..
eg: from my document and copy it to other destination such as destop..

//my coding
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
Dim sReadFile As String
Dim sBil As String
Dim staff As String

sReadFile = "C:\Documents and Settings\cmg\Desktop\STAFF.txt" 'filename

Dim sw As StreamWriter = New StreamWriter(sReadFile, True)
sBil = 0

Dim MyConnection As New OdbcConnection(MyConString)
MyConnection.Open()

Dim MyCommand As New OdbcCommand
MyCommand.Connection = MyConnection

MyCommand.CommandText = "SELECT StaffNo from meter_reader"

Dim MyDataReader As OdbcDataReader
MyDataReader = MyCommand.ExecuteReader
Do While MyDataReader.Read
staff = MyDataReader("staffno")

If staff <> "" Then
sw.WriteLine(staff & ";")
End If
Loop

sw.Close()

MsgBox("Text file created", MessageBoxIcon.Information, "Created")
End Sub
// end of mycoding
Was This Post Helpful? 0
  • +
  • -

#4 Rozie0910  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 13-October 08

Re: VB.NET File Transfer

Posted 15 October 2008 - 06:06 PM

[quote name='whisla13' post='226541' date='22 May, 2007 - 10:45 AM']
Transferring a file can be as simple as reading data from one stream, and writing it to another. Using this code:

Public Sub TransferData(FromStream As IO.Stream, ToStream As IO.Stream, BufferSize As Integer)
	 Dim buffer(BufferSize - 1) As Byte
	 Do While True
		  Dim bytesRead As Integer = FromStream.Read(buffer, 0, buffer.Length)
		  If bytesRead = 0 Then Exit Do
		  ToStream.Write(buffer, 0, bytesRead)
	 Loop
End Sub



could u help me how to use this code.
i dont understand

This post has been edited by Rozie0910: 15 October 2008 - 06:08 PM

Was This Post Helpful? 0
  • +
  • -

#5 sscheider  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 55
  • Joined: 14-May 09

Re: VB.NET File Transfer

Posted 19 July 2009 - 11:48 AM

View Postwhisla13, on 22 May, 2007 - 09:45 AM, said:

Public Sub TransferData(FromStream As IO.Stream, ToStream As IO.Stream, BufferSize As Integer)
	 Dim buffer(BufferSize - 1) As Byte
	 Do While True
		  Dim bytesRead As Integer = FromStream.Read(buffer, 0, buffer.Length)
		  If bytesRead = 0 Then Exit Do
		  ToStream.Write(buffer, 0, bytesRead)
	 Loop
End Sub


Howdy Andrew,
I would suggest adding a bit to this code. As written, receiving software can overrun the hardware speed. I ran across this with a TCP listener. In order to fix this, I check if the number of received bytes is less than a full buffer length. If so, then give up the thread for the remainder of its slice. Hopefully, the next slice will allow the hardware to catch up and put some more data in the buffer. Also, since this is pretty close to the hardware, I added an exception trap. Here's a revised version:
	Public Function TransferData(ByVal FromStream As IO.Stream, ByVal ToStream As IO.Stream, ByVal BufferSize As Integer) As Long
		Dim buffer(BufferSize - 1) As Byte
		Dim numBytesRead As Integer
		Dim done As Boolean
		Dim totalBytes As Long

		totalBytes = 0
		Try
			If FromStream.CanRead And ToStream.CanWrite Then
				done = False
				While Not done
					numBytesRead = FromStream.Read(buffer, 0, buffer.Length)

					'add up the bytes transferred'
					totalBytes = totalBytes + numBytesRead

					If numBytesRead = 0 Then
						done = True
					Else 
						ToStream.Write(buffer, 0, numBytesRead)

						'give up the thread if the buffer was incomplete'
						If numBytesRead < buffer.Length then Thread.Sleep(0)
					End If ' of if theres no more data'

				End While
			Else
				totalBytes = -1 ' config problem '
			End If ' of if the streams currently can pass data'
		Catch ex As Exception
			totalBytes = -1 ' let the caller know about the problem'
		End Try ' of try/catch'

		TransferData = totalBytes
	End Function




I hope this refinement helps!
Steve

This post has been edited by sscheider: 19 July 2009 - 02:28 PM

Was This Post Helpful? 1

Page 1 of 1