I'm creating my second application. I'm not very good at coding. If i make dump mistake please bare with me.

I can't get this Progress bar to work, I tried few steps. I'm writing this application to copying the Host file to a folder in desktop for troubleshooting purpose at Office. (there are few more if i can get this to work i can replicate the same for others)

This is the code

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim USERPROFILE As String
        If ProgressBar1.Value < ProgressBar1.Maximum Then
            ProgressBar1.Value += 100
        Else
            ProgressBar1.Value = ProgressBar1.Minimum
        End If
        USERPROFILE = Environ("HOMEPATH")
        IO.Directory.CreateDirectory(USERPROFILE & "\Desktop\TechSupport")
        Dim oShell : oShell = CreateObject("WScript.shell")
        oShell.Run("CMD /C copy %windir%\system32\drivers\etc\HOSTS C:\hosts.txt", 0, True)
        My.Computer.FileSystem.CopyFile("C:\hosts.txt", USERPROFILE & "\Desktop\TechSupport\hosts.txt",
        Microsoft.VisualBasic.FileIO.UIOption.AllDialogs,
        Microsoft.VisualBasic.FileIO.UICancelOption.DoNothing)
        My.Computer.FileSystem.DeleteFile("C:\hosts.txt")
        MsgBox("Thank You for your Patience.")
        
    End Sub

    Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click

    End Sub

What modification i have to make to get the Progress bar correctly.

Thanks in advance

Please explain what you are trying to do. The code does not make sense to me.

If the value is between Min and Max then it will go to 0

Your ELSE statement will never execute because if the value is greater then the Max it will Error.

Hi captain Jack

THis all seems a lot of hard work considering the purpose is merely to copy a file. How big is your Hosts file? If it is at least tens of megabytes then perhaps we'd understand the need for a progress bar. Perhaps it is taking a long time because you are using both shell and my.computer.filesystem to do the copying - and then deleting - when, as far as I can see, you only need to do one copy.

I think this is a simpler way to do things:

Public Sub CopyHostFile()
        Try
            Dim sourcePath As String = Environment.SystemDirectory & "\drivers\etc\HOSTS"
            Dim targetPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\TechSupport\Hosts.txt"
            My.Computer.FileSystem.CopyFile(sourcePath, targetPath, True)
            MsgBox("Copy complete")
        Catch ex As Exception
            MsgBox("Error copying file: " & ex.Message)
        End Try
    End Sub

If you absolutely MUST show progress (I suspect this is your reason for doing things the way you have), try this:

Public Sub CopyHostFile()
        Try
            Dim sourcePath As String = Environment.SystemDirectory & "\drivers\etc\HOSTS"
            Dim targetPath As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\TechSupport\Hosts.txt"
            If My.Computer.FileSystem.FileExists(targetPath) Then
                My.Computer.FileSystem.DeleteFile(targetPath)
            End If
            My.Computer.FileSystem.CopyFile(sourcePath, targetPath, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
            MsgBox("Copy complete")
        Catch ex As Exception
            MsgBox("Error copying file: " & ex.Message)
        End Try
    End Sub
commented: Good Help ! +0
Imports System.IO
ProgressBar1.Value = 0                                                                  'Set Progress Bar To Nothing'

        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\TechSupport")           'Create The Directory You Wish To Use'
        Dim DestFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\TechSupport\Hosts.txt"   'Declare DestFile as your NEW File'
        Dim OrigFile As String = Environment.GetFolderPath(Environment.SpecialFolder.System) & "\drivers\etc\HOSTS"        'Declare OrigFile as your OLD File'
        If File.Exists(DestFile) Then                                                           'If The File Already Exists'
            File.Delete(DestFile)                                                               'Delete It'
        End If
        File.Copy(OrigFile, DestFile)                                                           'Copy The File'

        ProgressBar1.Value = 100                                                                'Set Progress Bar To Full'

Just Use System.IO.File.(Copy,Delete) To Make Your Changes, Is SHELLing Necessary For Such A Text File? In My Experiences The HOSTS File Has Been Less Then 1Mb At Max.

You Can Also Add Message Boxes For Confirmation If You Wish, And A Try Statement Around The Code So The The Program Will Not Crash If A Problem Arises Copying The File.

Also, be sure to always comment on your code as i have, as in the future it will save you when you get stuck as in more advanced codes, it will enable other members to read it much easier.
~Matt

commented: Good help .. Thanks :) +0

Hello,

Thanks MrClark and crapulency. There are few other option which i need Progress Bar now i get the idea how to create one. Thanks. MrClark thanks for updating the code i'm very new to this just have some basic knowledge of C++ that's all rest i picked up from Google :) Its a office thing i agree to do this so i might get considered for a promotion. Thanks again you have no idea how much this helps me.

Regards !

No problem, if you ever need help with any program, just ask, any member here will be glad to help providing you put in the first effort.
~Matt

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.