Need some help with System.IO.File.Copy()

AntonioHernandez

New member
Joined
Oct 2, 2007
Messages
3
Location
Tucson, AZ
Programming Experience
1-3
Hello all, I am fairly new to writing VB.NET and would really appreciate some help with a project I am working on. I am trying to develop a program that will copy a zip file from a local machine to a share on a remote machine. I am using the "OpenFileDialog" to select the file. The problem I am running into is the OpenFileDialog.FileName contains the entire path of the file. I only need the file name when using System.IO.File.Copy() and need to figure out a way to remove the path of the selected file. Here is my code...


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

Dim strFileName As String
Dim strDestLocation As String

OpenFileDialog1.InitialDirectory = "C:\ZIP-Files\"
OpenFileDialog1.Title = "Please Select a ZIP File"
OpenFileDialog1.Filter = "ZIP Files|*.zip"
OpenFileDialog1.ShowDialog()

strFileName = OpenFileDialog1.FileName
strDestLocation = "\\ServerName\ShareName\" & (strFileName)

System.IO.File.Copy(strFileName, strDestLocation)


End Sub
 
try IO.Path.GetFilename function
 
When working with a file, I always like to create a fileinfo.

This way you have easy access to all the file attributes.

Dim fi As New System.IO.FileInfo(strfilename)

' Then to just get the name of the file use
' fi.Name()
 
Now the Progress Bar...

First I want to thank both vinnie881 and JohnH for their suggestions. I ended up using the IO.Path.GetFilename function. Works like a champ! The last thing I need to figure out "Which I'm sure you will crunge when I mention it" is how to use a Progress Bar with the System.IO.File.Copy function. I understand that there is a bit of a "loop" funtion I need to use to keep checking the status of the progress but I havn't been able to find any good examples online. Once again I am all ears and willing to do any research to get this done. Below is an example of my almost completed code:

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

        Dim strFileName As String
        Dim strDestLocation As String

        OpenFileDialog1.InitialDirectory = "C:\ZIP-Files\"
        OpenFileDialog1.Title = "Please Select a ZIP File"
        OpenFileDialog1.Filter = "ZIP Files|*.zip"
        OpenFileDialog1.ShowDialog()

        strFileName = System.IO.Path.GetFileName(OpenFileDialog1.FileName)
        strDestLocation = "\\ServerName\ShareName\" & (strFileName)

        System.IO.File.Copy(strFileName, strDestLocation, True)

    End Sub
 
Last edited by a moderator:
My.Computer.FileSystem.CopyFile method have two overloads where you can set UIOption to show progress (also allows cancel). Otherwise you have to work with IO.FileStream, reading and writing smaller byte chunks of the file while you update progress.
 
How to use the UICancelOption

The "My.Computer.FileSystem.CopyFile" is a great alternative, but I am having trouble figuring out how to "End Sub" if the Cancel button is pressed...

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

Dim strFileName As String
Dim strDestLocation As String

OpenFileDialog1.InitialDirectory = "C:\ZIP-Files\"
OpenFileDialog1.Title = "Please Select a ZIP File"
OpenFileDialog1.Filter = "ZIP Files|*.zip"
OpenFileDialog1.ShowDialog()

strFileName = OpenFileDialog1.FileName
strDestLocation = "\\ServerName\ShareName\" & (strFileName)

My.Computer.FileSystem.CopyFile(strFileName, strDestLocation, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs)


End Sub
 
Last edited by a moderator:
Default UICancelOption is ThrowExecption, you can change it to DoNothing if you don't care if file copy is cancelled, else you must Try-Catch that call.
 
Back
Top