ArgumentException was unhandled

insomnica

Member
Joined
Nov 10, 2007
Messages
10
Programming Experience
1-3
Im getting an "ArgumentException was unhandled" error when trying to save a document written in my application.
Says "Empty path name is not legal"

I know its a problem with this sub because I get all other functions working.

Codus Problamaticus
VB.NET:
 Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim SW As New IO.StreamWriter(SaveFileDialog1.FileName)
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            SW.Write(TextBox1.Text)
            SW.Flush()
            SW.Close()
            FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
            Print(1, TextBox1.Text)
            FileClose(1)
        End If
    End Sub

If you need any more code just give me a shout.
EDIT: This was written in VB.NET 2005 and im using 2008 now, could that be the problem? Worked perfect in 2005, but ive upgraded
 
Last edited:
change click code to this:
VB.NET:
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   Dim SW As New IO.StreamWriter(SaveFileDialog1.FileName)
   SW.Write(TextBox1.Text)
   SW.Close()
End If
or even simpler:
VB.NET:
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
   My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
End If
In case you also didn't understand the problem, you were accessing SaveFileDialog1.FileName before you had displayed the dialog.

Since you don't depend on other dialog result than 'ok' or using the same dialog for different purposes you can doubleclick the SaveFileDialog1 component in designer to get to its FileOK event where you put the save code. So in your click you just call the dialog:
VB.NET:
SaveFileDialog1.ShowDialog()
then use the event:
VB.NET:
Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
   My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
End Sub
 
Thanks again John!
Ive got it working, im going to Filter my OpenFileDialog now, then 2nd Beta it. :D
 
Back
Top