Stream Read and Write in vb.net
  • 'Name Spaces must be add 

    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Security.Principal
    Imports System.Security.Permissions
    Imports Microsoft.VisualBasic
    Imports Microsoft.Win32.SafeHandles
    Imports System.Runtime.ConstrainedExecution
    Imports System.Security
    Imports System.IO

    Public Class Form1
    Dim onlyfilename As String

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim Source As String = "\\192.168.0.170\Iron Speed Tutorials"


    If System.IO.File.Exists(Source + "/" + onlyfilename) = True Then
    MessageBox.Show("Same filename already exist")
    Else
    Try
    Dim fsSource As FileStream = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read)
    Dim data As Byte() = File.ReadAllBytes(TextBox1.Text)


    Dim bytesread As Integer = 0
    bytesread = fsSource.Read(data, 0, data.Length)
    Dim fsNew As FileStream = New FileStream(Source + "/" + onlyfilename, FileMode.Create, FileAccess.Write)
    fsNew.Write(data, 0, bytesread)


    fsSource.Close()
    fsNew.Close()
    MessageBox.Show("Uploded Succesfully")
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    End Try


    End If
    End Sub

    'Here is the event to get file from which data is read


    Private Sub btn_openfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_openfile.Click


    Dim dlg As New OpenFileDialog
    If dlg.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    TextBox1.Text = dlg.FileName
    Dim a() As String = TextBox1.Text.Split("\")
    onlyfilename = a(a.Length - 1)


    End If
    End Sub

    'Here is the event from where file is saved 


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim dlg As New FolderBrowserDialog


    If dlg.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    txt_folderpath.Text = dlg.SelectedPath
    End If


    End Sub

    Private Sub btn_download_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_download.Click

      Dim Source As String = "\\192.168.0.1010\deployQA\deploy\Release\Attachments"
    Try
    Dim fsSource As FileStream = New FileStream(Source + "/" + txt_filedownload.Text, FileMode.Open, FileAccess.Read)
    fsSource = File.OpenRead(Source + "/" + txt_filedownload.Text)
    Dim data As Byte() = File.ReadAllBytes(Source + "/" + txt_filedownload.Text)



    Dim bytesread As Integer = 0
    bytesread = fsSource.Read(data, 0, data.Length)

    Dim fsNew As FileStream = New FileStream(txt_folderpath.Text + "/" + txt_filedownload.Text, FileMode.Create, FileAccess.Write)

    fsNew.Write(data, 0, bytesread)

    fsSource.Close()
    fsNew.Close()
    MessageBox.Show("Download Succesfully")
    Catch ex As Exception

    MessageBox.Show(ex.Message)
    End Try
    End Sub

    Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim dlg As New OpenFileDialog


    If dlg.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    Dim a() As String = dlg.FileName.Split("\")


    txt_filedownload.Text = a(a.Length - 1)
    End If
    End Sub


    End Class

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week