Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 136,947 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,885 people online right now. Registration is fast and FREE... Join Now!




VB.NET File Transfer

 
Reply to this topicStart new topic

VB.NET File Transfer, How to transfer files using VB.NET

user22
30 Apr, 2007 - 11:17 AM
Post #1

New D.I.C Head
*

Joined: 30 Apr, 2007
Posts: 1


My Contributions
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.dreamincode.net/forums/showtopic21431.htm

CODE

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.
User is offlineProfile CardPM
+Quote Post

whisla13
RE: VB.NET File Transfer
22 May, 2007 - 09:45 AM
Post #2

New D.I.C Head
*

Joined: 22 May, 2007
Posts: 10


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

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 - 09:46 AM
User is offlineProfile CardPM
+Quote Post

Rozie0910
RE: VB.NET File Transfer
13 Oct, 2008 - 10:19 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
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
User is offlineProfile CardPM
+Quote Post

Rozie0910
RE: VB.NET File Transfer
15 Oct, 2008 - 05:06 PM
Post #4

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 19


My Contributions
[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:

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 Oct, 2008 - 05:08 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 11:24PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month