Newbie here
-
So how do i code it to copy the chosen file to another destination?
Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\") EndIf ' Will copy the chosen file to C:\
-
Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\") EndIf ' Will copy the chosen file to C:\
Thanks guys..another question.. Can i change the filename while copying the file to the new location? So that it will be renamed when it is copied to the new location?
-
Thanks guys..another question.. Can i change the filename while copying the file to the new location? So that it will be renamed when it is copied to the new location?
-
Hmm..but it doesn't teach us how to rename a file? Sorry but i'm really weak in Vb.Net >.<
-
Hmm..but it doesn't teach us how to rename a file? Sorry but i'm really weak in Vb.Net >.<
-
Private Sub MenuItem16_Click(ByVal sender as System.Object,ByVal e as EventArgs)Handles MenuItem16.Click Dim FileDialog As OpenFileDialog Dim Test As New Test 'The Test class from MSDN if FileDialog.ShowDialog() = DialogResults.Ok then Test.Main() End If There's no build error, but after I chose a file from the dialog box, no file was copied to the new location :( What's my problem here? Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\") EndIf ' Will copy the chosen file to C:\ I tried this as well, but after clicking OK in the dialog box, it gives an error indicating "Directory Exists", so i couldn't copy my chosen file to C:\ as well...why?
-
Private Sub MenuItem16_Click(ByVal sender as System.Object,ByVal e as EventArgs)Handles MenuItem16.Click Dim FileDialog As OpenFileDialog Dim Test As New Test 'The Test class from MSDN if FileDialog.ShowDialog() = DialogResults.Ok then Test.Main() End If There's no build error, but after I chose a file from the dialog box, no file was copied to the new location :( What's my problem here? Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\") EndIf ' Will copy the chosen file to C:\ I tried this as well, but after clicking OK in the dialog box, it gives an error indicating "Directory Exists", so i couldn't copy my chosen file to C:\ as well...why?
Nguyen Dinh Quy wrote: Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\") EndIf ' Will copy the chosen file to C:\ Try
Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\a.txt") EndIf
Work hard, Work effectively.
-
Nguyen Dinh Quy wrote: Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\") EndIf ' Will copy the chosen file to C:\ Try
Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\a.txt") EndIf
Work hard, Work effectively.
Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\a.txt") EndIf Yah, this will create a text file in C:\...but what if the user chooses other types of files? (.exe, .pdf) I tried choosing an .exe file, but it creates a .txt file, using notepad to read the .exe file. :( Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\" + dlg.FileName) EndIf I tried this, but it gives an error indicating "The given path's format is not supported".
-
Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\a.txt") EndIf Yah, this will create a text file in C:\...but what if the user chooses other types of files? (.exe, .pdf) I tried choosing an .exe file, but it creates a .txt file, using notepad to read the .exe file. :( Dim dlg as New OpenFileDialog if dlg.ShowDialog() = DialogResult.Ok then System.IO.File.Copy(dlg.FileName, "C:\" + dlg.FileName) EndIf I tried this, but it gives an error indicating "The given path's format is not supported".
Nguyen Dinh Quy wrote: Yah, this will create a text file in C:\...but what if the user chooses other types of files? (.exe, .pdf) I tried choosing an .exe file, but it creates a .txt file, using notepad to read the .exe file. Do you mean you want do copy file dynamically, where the user can choose which file to be copied and where? :confused: If the file you want to copy is .exe then the destination file must also .exe so
System.IO.File.Copy(dlg.FileName, "C:\a.exe")
Nguyen Dinh Quy wrote: ystem.IO.File.Copy(dlg.FileName, "C:\" + dlg.FileName) ifdlg.FileName="c:\a.txt"
then the destination file you use"C:\" + dlg.FileName
, then it means that you're copying file to "C:\c:\a.txt".
Work hard, Work effectively.
-
Nguyen Dinh Quy wrote: Yah, this will create a text file in C:\...but what if the user chooses other types of files? (.exe, .pdf) I tried choosing an .exe file, but it creates a .txt file, using notepad to read the .exe file. Do you mean you want do copy file dynamically, where the user can choose which file to be copied and where? :confused: If the file you want to copy is .exe then the destination file must also .exe so
System.IO.File.Copy(dlg.FileName, "C:\a.exe")
Nguyen Dinh Quy wrote: ystem.IO.File.Copy(dlg.FileName, "C:\" + dlg.FileName) ifdlg.FileName="c:\a.txt"
then the destination file you use"C:\" + dlg.FileName
, then it means that you're copying file to "C:\c:\a.txt".
Work hard, Work effectively.
Ahh i guess my first post was horribly phased ;P Anyway yah, users can choose from the dialog box which files to be copied, but the location where the files are copied to is fixed. With "Files", i mean any types of files.. e.g. User chooses Example.txt from C:\, and it is copied to D:\.So now there's C:\Example.txt and D:\Example.txt. The 1st question here is that i don't know how to do it.
-
Ahh i guess my first post was horribly phased ;P Anyway yah, users can choose from the dialog box which files to be copied, but the location where the files are copied to is fixed. With "Files", i mean any types of files.. e.g. User chooses Example.txt from C:\, and it is copied to D:\.So now there's C:\Example.txt and D:\Example.txt. The 1st question here is that i don't know how to do it.
The following code will handle copying of any file extension.
Dim FileDialog As New OpenFileDialog 'FileInfo class has Dim myFile as System.IO.FileInfo With FileDialog if .ShowDialog() = DialogResults.Ok then 'create FileInfo instance where .Filename is the fullpath of source 'file. For example: C:\Example.txt myFile = new System.IO.FileInfo(.Filename) 'FileInfo.Name property indicates the file name 'For example: If the source file is C:\Example.txt than the FileInfo.Name 'property is Example.txt myFile.CopyTo("D:\" & myFile.Name, false) 'syntax: FileInfo.CopyTo(destfilename as String, forceoverwrite as Boolean) End If End With
I hope this makes sense :-D A.S.