Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
VB
Dim sfd As New SaveFileDialog
            sfd.Filter = "Text Document (.txt)|*.txt"
            Dim sv As String
            sfd.ShowDialog()
            sv = sfd.FileName
            Dim sw As New StreamWriter(sv) <---ERROR HERE
                sw.Write(RichTextBox1.Text)
            sw.Close()

the error that pops up
{"Empty path name is not legal."}
I'm using vs 2010 Express
Posted
Updated 20-Sep-14 5:38am
v2

You should check the return value of ShowDialog and only look do something if the return value means that the user has pressed OK.
The user can also cancel the operation and then sfd.FileName will be empty.
VB
if (sfd.ShowDialog() == DialogResult.OK) // Sorry for the c# syntax
{
    Dim sw As New StreamWriter(sfd.FileName)
    sw.Write(RichTextBox1.Text)
    sw.Close()
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 14:14pm    
5ed.
—SA
The message is clear: sv is an empty string (while it must contain a valid path name).
You have to properly handle such scenarios (for instance when the user clicks on the SaveFileDialog's 'Cancel' button).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Sep-14 14:14pm    
5ed.
—SA
CPallini 20-Sep-14 16:22pm    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900