Search code examples
vb.netvisual-studiofilepdfio

Open a PDF existing file then save it in to a specific folder in VB.NET


I have existing code here, but it shows a SaveAs dialog immediately and this is quite confusing for some users. How can I improve this?

Dim write As StreamWriter
SaveFileDialog1.Filter = "PDF Files |*.pdf"
SaveFileDialog1.ShowDialog()
write = File.AppendText(SaveFileDialog1.FileName)
write.WriteLine()
write.Close()

Solution

  • If you want us to help you change how the user interface works, we need to see more context from that interface, and you need to talk to your users more about what they expected instead of the SaveAs dialog.

    But we can help improve this code some:

    SaveFileDialog1.Filter = "PDF Files |*.pdf"
    If SaveFileDialog1.ShowDialog() = DialogResult.OK AndAlso Not String.IsNullOrWhitespace(SaveFileDialog1.FileName) Then    
        Using writer As StreamWriter = File.AppendText(SaveFileDialog1.FileName) 
            writer.WriteLine()
        End Using
    End If
    

    Based on the question title, maybe you wanted to show an OpenFileDialog first? Have you tried that? And then maybe a FolderBrowserDialog instead of SaveFileDialog?