A reader wanted to know how to show a progress bar while copying a file.
This could be tough. The regular ProgressBar component uses a Value property to show progress. In order to use it, you would have to be able to monitor the number of bytes that have been copied and divide by the total number of bytes to plug into that property. And you would also have to be able to continuously poll an updated value of the number of bytes copied in real time. There might be a Windows API that does this but I don't know what it is. If you do, let me know!
Fortunately, the VB.NET CopyFile object has made it easy since Framework 2.0. In fact, it's so easy that I felt like I had to throw in a few more examples to make this Quick Tip worth reading, so I also included examples of OpenFileDialog and FolderBrowserDialog.
The whole application consists of just three buttons. The first calls the OpenFileDialog to select the file to be copied. The second calls the FolderBrowserDialog to select a folder to copy the file. And the third just does the copy.
Here's the code for the first button - OpenFileDialog:
FileFromDialog.InitialDirectory = "c:\" FileFromDialog.Filter = _ "txt files (*.txt)|*.txt|All files (*.*)|*.*" dlgResult = FileFromDialog.ShowDialog() If dlgResult System.Windows.Forms.DialogResult.OK Then MessageBox.Show("File Error " & dlgResult) End If
You have probably seen the OpenFileDialog in a thousand other applications that you use. The OpenFileDialog is powerful enough to check for the existance of files and folders, look for ReadOnly files, and even look for "multidotted extensions". (Which means just what it looks like. A file like "FileName.ext1.ext2.ext3". Yes, they do exist.)
In the case above, I specify that .txt or all files can be shown. You can set file filter default to any of of the listed file filter options using the FilterIndex property. It defaults to 1, the first filter in the list. The If statement is just a very simple check to see if a file was successfully selected.
The code for the second button - FolderBrowserDialog - is very similar:
dlgResult = FolderToDialog.ShowDialog() If dlgResult System.Windows.Forms.DialogResult.OK Then MessageBox.Show("Folder Error " & dlgResult) End If
And the code for the third button actually copies the file using the very handy My.Computer.FileSystem.CopyFile object. I've continued this onto five lines to keep the lines short but it's just one statement:
My.Computer.FileSystem.CopyFile( _ FileFromDialog.FileName, _ FolderToDialog.SelectedPath & "\" & _ FileFromDialog.SafeFileName, _ FileIO.UIOption.AllDialogs)
There are really only two things of note here. The first is that I had to build the output path using the path selected with FolderBrowserDialog and the name selected with OpenFileDialog. The FileName property of OpenFileDialog has the path in it. SafeFileName just has the name. But the second is the key to the reader's question. FileIO.UIOption.AllDialogs shows a progress bar automatically!
Ordinarily on most computer and with most files, a CopyFile operation is so quick that the progress bar is never shown. So to test it, I had to find a 200,000KB file on my system to copy - actually not too hard with the downloads that Microsoft wants you to do these days. The SQL Server Management Studio download was plenty big enough. Here's the copy in action:
Click Here to display the illustration
Click the Back button on your browser to return