Question Need Help with Dialog File

bagwis

Member
Joined
Apr 18, 2012
Messages
8
Programming Experience
1-3
Hello, good day, I just need some help here, I have found n the net this line of codes here:
VB.NET:
Using openDialog As New OpenFileDialog()
            openDialog.CheckFileExists = True
            openDialog.CheckPathExists = True
            openDialog.Filter = "Text Document (*.txt)|*.txt"

            Using saveDialog As New SaveFileDialog()
                saveDialog.CheckFileExists = False
                saveDialog.CheckPathExists = True
                saveDialog.FileName = ("MyFile") & ".txt"
                saveDialog.Filter = "Text Document (*.txt)|*.txt"

                Try
                    If openDialog.ShowDialog() = Windows.Forms.DialogResult.OK AndAlso saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        IO.File.Copy(openDialog.FileName, saveDialog.FileName, True)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End Using
        End Using
The purpose of this code is to copy a certain file and save it to another directory using open and save dialog, this code works well but I what I want to be done is that after selecting the file that I want to copy it will be just automatically saved into the specific directory that I set wherein it will be named according to what I set also in the code. Say for example I want to copy the file named MyTxt.txt and once I select this file it will be automatically saved in C:\MyFiles as MyFile.txt. I spend my whole day today just to figure out how to make this but still no good. Thanks to someone who can help me out. :)
 
Hello, good day, I just need some help here, I have found n the net this line of codes here:
VB.NET:
Using openDialog As New OpenFileDialog()
            openDialog.CheckFileExists = True
            openDialog.CheckPathExists = True
            openDialog.Filter = "Text Document (*.txt)|*.txt"

            Using saveDialog As New SaveFileDialog()
                saveDialog.CheckFileExists = False
                saveDialog.CheckPathExists = True
                saveDialog.FileName = ("MyFile") & ".txt"
                saveDialog.Filter = "Text Document (*.txt)|*.txt"

                Try
                    If openDialog.ShowDialog() = Windows.Forms.DialogResult.OK AndAlso saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        IO.File.Copy(openDialog.FileName, saveDialog.FileName, True)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End Using
        End Using
The purpose of this code is to copy a certain file and save it to another directory using open and save dialog, this code works well but I what I want to be done is that after selecting the file that I want to copy it will be just automatically saved into the specific directory that I set wherein it will be named according to what I set also in the code. Say for example I want to copy the file named MyTxt.txt and once I select this file it will be automatically saved in C:\MyFiles as MyFile.txt. I spend my whole day today just to figure out how to make this but still no good. Thanks to someone who can help me out. :)

Hello

You are just about there you just need to get rid of the savedialog. Try this
Dim FolderName As String = "C:\MyFiles\"
Using openDialog As New OpenFileDialog()
openDialog.CheckFileExists = True
openDialog.CheckPathExists = True
OpenDialog.CheckPathExists = True
openDialog.Filter = "Text Document (*.txt)|*.txt
Try
If openDialog.ShowDialog() = Windows.Forms.DialogResult.OK then
IF IO.Directory.Exists(FolderName) Then
IO.File.Copy(openDialog.FileName, FolderName & "MyFile.txt")
Else
If MsgBox("The Folder does not exist, would you like to create it", MsgBoxStyle.YesNo,"Folder Error") = MsgBoxResult.Yes Then
IO.Directory.CreateDirectory(FolderName)
IO.File.Copy(openDialog.FileName, FolderName & "MyFile.txt", True)
End If
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using


You could also try importing the System.IO then you would not have to keep refering to it.
If this is not indented I apologise, I am not sure how to do this, I have tried everything even using
 
Last edited:

Latest posts

Back
Top