Okay hi people i just want to is it possible if you can use a Certain copy / Write Function
that writes your file from Resources to where you want but with Progress(Progress bar) while its copying or Writing and if so can you give me a example please:'(

Recommended Answers

All 18 Replies

Member Avatar for Unhnd_Exception

To show progress you will need to get streams and read and write to them so you can tell how many bytes have been written.

I don't know how to get a stream from my.resources but I do know a couple of things.

You can add the file as an embedded resource. To do: Add a file to the project and set its BuildAction to Embedded Resource. You can leave the Copy to output directory to Do Not Copy.

The below example shows how to get a stream from the embedded resource.


You could also add a file to your project and set its Build Action to Content and Copy to Output Directory to Copy If Newer.

Then you could get a FileStream with

Dim FileStream as New IO.FileStream("Test.txt", IO.FileMode.Open)

If you set it as an embedded resource the file will be part of the .exe file. If you set the BuildAction to content and Copy To Output to Copy if Newer the file will be located in the application directory.

At any rate copying a file showing a progress will look something like below.

Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'make sure you set this so the progress will change.
        'you can do this on the designer and delete this line.
        BackgroundWorker1.WorkerReportsProgress = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Get a stream to the file to copy.
        'Depicts getting a stream from an embedded resource.
        Dim Assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
        Dim EmbeddedStream As IO.Stream = Assembly.GetManifestResourceStream(Me.GetType, "Test.txt")

        If EmbeddedStream IsNot Nothing Then

            Dim SaveFileDialog As New SaveFileDialog

            If SaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

                'Create a new file stream from the file name selected.
                Dim NewFileStream As New IO.FileStream(SaveFileDialog.FileName, IO.FileMode.Create)
                'Put the streams into an array so you can pass them
                'to the background worker.
                Dim Streams() As IO.Stream = {EmbeddedStream, NewFileStream}

                ProgressBar1.Value = 0
                BackgroundWorker1.RunWorkerAsync(Streams)

            End If

            SaveFileDialog.Dispose()
        End If

    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        'How ever you get your streams your copy code will look like this.

        'You should verify that e.argument is an array of streams
        'and it has two streams
        Dim CopyStream As IO.Stream = CType(e.Argument, IO.Stream())(0)
        Dim NewStream As IO.Stream = CType(e.Argument, IO.Stream())(1)

        'The buffer length can be any number.
        'I chose 100.  This means it will read and write
        '100 bytes at a time.
        Dim Buffer(99) As Byte
        Dim BytesRead As Integer
        Dim TotalBytesRead As Integer
        Dim PercentComplete As Integer

        Try

     
            Do
                'Read up to the buffer length.
                BytesRead = CopyStream.Read(Buffer, 0, Buffer.Length)
                'Now write to all the bytes read in the buffer. 
                NewStream.Write(Buffer, 0, BytesRead)
                'Increment the total so you can keep track of progress.
                TotalBytesRead += BytesRead

                PercentComplete = CInt(TotalBytesRead / CopyStream.Length * 100)

                'You don't want to update the progress bar with every write.
                'This line will update the progress bar every 5 percent.
                If PercentComplete Mod 5 = 0 Then
                    BackgroundWorker1.ReportProgress(PercentComplete)
                End If


            Loop Until BytesRead = 0
        Catch ex As Exception

        Finally
            'Put this in the finally block to ensure the streams
            'are always disposed of.
            CopyStream.Dispose()
            NewStream.Dispose()
        End Try
       

    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

The ProgressBar's maximum is set to 100 on the designer.

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim AppName As String = "Memory freeo.exe"
        Dim Assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
        Dim EmbeddedStream As IO.Stream = Assembly.GetManifestResourceStream(Me.GetType, AppName)
        If EmbeddedStream IsNot Nothing Then
            'Create a new file stream from the file name selected.
            Dim NewFileStream As New IO.FileStream(Form4.USerFolderPicked & AppName, IO.FileMode.Create)
            'Put the streams into an array so you can pass them
            'to the background worker.
            Dim Streams() As IO.Stream = {EmbeddedStream, NewFileStream}
            ProgressBar1.Value = 0
            BackgroundWorker1.RunWorkerAsync(Streams)
        End If
        Timer2.Stop()
    End Sub 

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        'How ever you get your streams your copy code will look like this.
        'You should verify that e.argument is an array of streams
        'and it has two streams
        Dim CopyStream As IO.Stream = CType(e.Argument, IO.Stream())(0)
        Dim NewStream As IO.Stream = CType(e.Argument, IO.Stream())(1)
        'The buffer length can be any number.
        'I chose 100. This means it will read and write
        '100 bytes at a time.
        Dim Buffer(99) As Byte
        Dim BytesRead As Integer
        Dim TotalBytesRead As Integer
        Dim PercentComplete As Integer
        '    Try
        Do
            'Read up to the buffer length.
            BytesRead = CopyStream.Read(Buffer, 0, Buffer.Length)
            'Now write to all the bytes read in the buffer.
            NewStream.Write(Buffer, 0, BytesRead)
            'Increment the total so you can keep track of progress.
            TotalBytesRead += BytesRead
            PercentComplete = CInt(TotalBytesRead / CopyStream.Length * 100)
            'You don't want to update the progress bar with every write.
            'This line will update the progress bar every 5 percent.
            If PercentComplete Mod 5 = 0 Then
                BackgroundWorker1.ReportProgress(PercentComplete)
            End If
        Loop Until BytesRead = 0
        '  Catch ex As Exception
        '  Finally
        CopyStream.Dispose()
        NewStream.Dispose()
        ' End Try
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        If Me.ProgressBar1.Value = ProgressBar1.Maximum Then
            Me.Close()
            Form6.Show()
        Else
        End If
    End Sub

Why is this not working its not writing the files and the folders

Member Avatar for Unhnd_Exception

Make sure the embedded stream is not nothing. If it is then make sure you have the build action of your Memory freeo.exe to embedded resource. Or get the stream some other way. The embedded resource was for example but will work.

On the new file stream make sure there is a "\" at the end of form4.userfolderpicked and make sure that folder exists.

You may need to set some break points in the code to see what the problem is.


You can try something simple to make sure its working.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Create a file to copy and a new file
        Dim FileToCopy As New IO.FileStream("C:\CopyFile.txt", IO.FileMode.Create)
        Dim NewFile As New IO.FileStream("C:\NewFile.txt", IO.FileMode.Create)

        'Write some garbage in the file to copy
        For i = 1 To 1000
            FileToCopy.WriteByte(5)
        Next

        'Close it out.
        FileToCopy.Flush()
        FileToCopy.Dispose()

        'FileToCopy now exists.  Reopen it and copy the file
        'showing progress.
        FileToCopy = New IO.FileStream("C:\CopyFile.txt", IO.FileMode.Open)

        Dim Streams() As IO.Stream = {FileToCopy, NewFile}

        ProgressBar1.Value = 0
        BackgroundWorker1.RunWorkerAsync(Streams)

End Sub

Okay thanks but ive got it working
But i only have one more question about your First Example
What if i had more than one file that was maybe in my resources
IS there a way to combine it without making 2 Streams ??

i HOPE IT POSSIBLE

:?:

iM SORRY for asking so much but i suck in this area

There's a lot to learn

:D

commented: This is not a chat client. Please don't string out your messages over 4 one-sentence posts. -4
Member Avatar for Unhnd_Exception

You mean copy two files into one file or copy more than one file at a time with the same progress bar?

More than one file

Sorry for bad Writing

WIth same Progress ofcourse

;)

Im going to use Embed like you said

Member Avatar for Unhnd_Exception

Something like this. This is all rough draft code so you may need to make so mods but it works.

I changed one thing up. The b worker takes a list of a new filecopyinfo class instead of the streams.

Private Class FileCopyInfo
        Public EmbeddedResourceName As String
        Public DestinationFilePath As String

        Sub New(ByVal embeddedResourceName As String, ByVal destinationFilePath As String)
            Me.EmbeddedResourceName = embeddedResourceName
            Me.DestinationFilePath = destinationFilePath
        End Sub

    End Class


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If BackgroundWorker1.IsBusy Then Exit Sub

        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = 100
        ProgressBar1.Value = 0

        Dim ResourcesToCopy As New List(Of FileCopyInfo)

        ResourcesToCopy.Add(New FileCopyInfo("Test.txt", "C:\Why.txt"))
        ResourcesToCopy.Add(New FileCopyInfo("Fill Outline.exe", "C:\WhyNot.exe"))
        'Add as many as needed

        BackgroundWorker1.RunWorkerAsync(ResourcesToCopy)
      
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        If e.Argument Is Nothing OrElse Not TypeOf e.Argument Is List(Of FileCopyInfo) Then
            Exit Sub
        End If
        Dim ListOfFilesToCopy As List(Of FileCopyInfo) = CType(e.Argument, List(Of FileCopyInfo))

        Dim Assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly
        Dim Buffer(99) As Byte
        Dim BytesRead As Integer
        Dim TotalBytesRead As Integer
        Dim PercentComplete As Integer
        Dim TotalBytes As Integer

        'Get the length of all resource files.
        Dim Stream As IO.Stream
        For Each FCI As FileCopyInfo In ListOfFilesToCopy
            Stream = Assembly.GetManifestResourceStream(Me.GetType, FCI.EmbeddedResourceName)
            If Stream Is Nothing Then
                Throw New Exception("It just won't work.")
            Else
                TotalBytes += Stream.Length
                Stream.Dispose()
                Stream = Nothing
            End If
        Next

        Dim CopyStream As IO.Stream
        Dim NewStream As IO.Stream

        'Now copy all the streams with the same progress bar.
        For Each FCI As FileCopyInfo In ListOfFilesToCopy

            CopyStream = Assembly.GetManifestResourceStream(Me.GetType, FCI.EmbeddedResourceName)
            NewStream = New IO.FileStream(FCI.DestinationFilePath, IO.FileMode.Create)

            Try


                Do
                    BytesRead = CopyStream.Read(Buffer, 0, Buffer.Length)
                    NewStream.Write(Buffer, 0, BytesRead)
                    TotalBytesRead += BytesRead

                    PercentComplete = CInt(TotalBytesRead / TotalBytes * 100)
                    If PercentComplete Mod 5 = 0 Then
                        BackgroundWorker1.ReportProgress(PercentComplete)
                    End If


                Loop Until BytesRead = 0

                NewStream.Flush()

            Catch ex As Exception
                Throw ex
            Finally
                'make sure the streams get disposed of or you will have issues.
                CopyStream.Dispose()
                NewStream.Dispose()
            End Try

        Next

    End Sub

Thanks Unhnd_Exception
You are the best:)

Thread Solved:icon_mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.