Question Correct path for System.IO.File.Copy

Belfa91

New member
Joined
Sep 10, 2011
Messages
2
Programming Experience
3-5
Hi, i got to do a program in VB.NET that copies a file from a pc to a usb device. I use the method System.IO.File.Copy(FileToCopy, NewCopy) in which i have to specify the destination path. The problem is that when i change pc the destination can change: for example in my pc i read my USB pendrive o G:\ and on my friend pc i read the usb pendrive on E:\. How can i solve that? May i specify a general path to my USB giving it a name or anything similar?
 
Use dialog boxes

You could use dialog boxes to display locations and filenames which user can select. Place the OpenFileDialog and SaveFileDialog controls in your project. They will appear below your form. You may use the following code (I named the dialog boxes ofdOpen and sfdSave.) The user selects the filename from the list or may type in a new filename to save:

VB.NET:
    Private Sub btnFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile.Click
        Dim filespec1, filespec2 As String
        ofdOpen.ShowDialog()        
        filespec1 = ofdOpen.FileName
        sfdSave.ShowDialog()         
        filespec2 = sfdSave.FileName
        If Not FileIO.FileSystem.FileExists(filespec2) Then
            IO.File.Copy(filespec1, filespec2)
        End If
    End Sub
 
Similar to the file open/save dialogs you could use the FolderBrowserDialog to let user select location if filename should not change.
If you only want to use the drive as location you can use DriveInfo class and list only removable drives in for example a combobox.
 
Mh, sorry, i did not specify that my program has to find the correct path on his own, without asking me to select it.
I thought of using recourisive scanning of the different drives (DriveInfo) and find a file created from me that tells it's the correct drive, but it would be very long because of it starts from C:\...so i thought of realtive path..in fact i launch the program from the pen drive where i have to save it, so if i don't specify the path, it uses the path from where it had been launched. Right?
 
Application.StartupPath could be used.
 
hi here's a litle code that may help, you will need to modify with "Application.StartupPath" which JohnH said.

Public Sub GetFilez()
Dim strFileSize As String = ""
Dim di As New IO.DirectoryInfo(sPaths)
Dim aryFi As IO.FileInfo() = di.GetFiles("*.*")
Dim fi As IO.FileInfo
For Each fi In aryFi
aFiles += fi.Name.ToString & Chr(32) 'Change here to copy files
Next
End Sub
 
Back
Top