kimberley35640
New member
- Joined
- Jun 20, 2005
- Messages
- 3
- Programming Experience
- Beginner
Problems with file I/O "Resolved"
I'm having some problems with a program I'm writing. I need to put in code to ensure that when I open a file dialog to select an input file that there is a guard against the user just hitting cancel instead of selecting a file. Then, in the save file dialog, I need to make sure that the user puts in a new file name to save the information to instead of the original file. I thought I'd figured it out but it still doesn't work. I'll post the code below.
I'm new to VB and programming so there may be some obvious solution to my problem that I'm just not seeing. I did a search and have been reading posts in forums and searching internet sites but it could be that I didn't recognise the answer when I saw it.
Here goes....
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim InFileName As String
Dim OutFileName As String
'prompt for the existing input file path
InFileName = GetTheFileName(cdlOpenFile, "Where is the Phone File?", FileFilter)
'prompt for the output file path
OutFileName = GetTheFileName(cdlSaveFile, "Specify Filename to Save", FileFilter)
'guard against user pressing cancel
If InFileName = DialogResult.OK Then
'open phone file to read
ContactReader = New System.IO.StreamReader(InFileName)
End If
'make sure save file is saved under differen name than input file
If OutFileName = InFileName Then
MsgBox("Save file must have a different name than the original file.")
Else
'open output file to write
ContactWriter = New System.IO.StreamWriter(OutFileName) End If
I just don't know what I'm doing wrong. I'm getting an error each time I test the program by pressing cancel or putting in the same save name as the input name.
This is the error...
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Double' is not valid.
Please help me if you can. I'm sooo frustrated and I feel like I have no clue what I'm doing.
Thanks in advance. Kimberley
I'm having some problems with a program I'm writing. I need to put in code to ensure that when I open a file dialog to select an input file that there is a guard against the user just hitting cancel instead of selecting a file. Then, in the save file dialog, I need to make sure that the user puts in a new file name to save the information to instead of the original file. I thought I'd figured it out but it still doesn't work. I'll post the code below.
I'm new to VB and programming so there may be some obvious solution to my problem that I'm just not seeing. I did a search and have been reading posts in forums and searching internet sites but it could be that I didn't recognise the answer when I saw it.
Here goes....
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim InFileName As String
Dim OutFileName As String
'prompt for the existing input file path
InFileName = GetTheFileName(cdlOpenFile, "Where is the Phone File?", FileFilter)
'prompt for the output file path
OutFileName = GetTheFileName(cdlSaveFile, "Specify Filename to Save", FileFilter)
'guard against user pressing cancel
If InFileName = DialogResult.OK Then
'open phone file to read
ContactReader = New System.IO.StreamReader(InFileName)
End If
'make sure save file is saved under differen name than input file
If OutFileName = InFileName Then
MsgBox("Save file must have a different name than the original file.")
Else
'open output file to write
ContactWriter = New System.IO.StreamWriter(OutFileName) End If
I just don't know what I'm doing wrong. I'm getting an error each time I test the program by pressing cancel or putting in the same save name as the input name.
This is the error...
An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll
Additional information: Cast from string "" to type 'Double' is not valid.
Please help me if you can. I'm sooo frustrated and I feel like I have no clue what I'm doing.
Thanks in advance. Kimberley
Last edited: