-3

I am creating a PDF form in vb.net and saving it to my desktop. The user inputs a name into the textbox and when they click the button the pdf file is created and saved to my desktop.

The issue I am having is that the pdf is being saved like this:

Desktop0001report.pdf

I do not understand why "Desktop" is showing in the front. Is there a way to fix this?

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Try
            Dim pdfdoc As New Document(PageSize.A4, 15, 15, 30, 30)

            Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + TextBox1.Text + " report.pdf"
            Dim file As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
            PdfWriter.GetInstance(pdfdoc, file)
            pdfdoc.Open()
            FillPDF(pdfdoc)
            pdfdoc.Close()
            Process.Start(filename)


        Catch ex As Exception
            MessageBox.Show("PDF could not be generated!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

End Sub

End Class
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
ARon118
  • 1
  • 2
  • Did you debug your app? Verify the value of `filename`? What was the value? – mason Jan 19 '21 at 22:04
  • @mason yes i did and this is what it brought back: "C:\Users\L00025\Desktop20201112_JW3 report.pdf " but it should be "C:\Users\L00025\Desktop\20201112_JW3 report.pdf " – ARon118 Jan 19 '21 at 22:16
  • 3
    Well, what's the difference? A backslash character (path separator), right? So now that you've figured out what's happening, you should know that you need to add a path separator somehow. One could use simple string concatenation. But there's a method on the Path class called [Path.Combine](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=net-5.0#System_IO_Path_Combine_System_String_System_String_) that will do it for you. I suggest you use that method. – mason Jan 19 '21 at 22:19
  • @mason well shouldnt it just be saving the file as 2020112_JW3 report.pdf? – ARon118 Jan 19 '21 at 22:30
  • Look at at the string you generated for filename. See how there's no path separator character between the "Desktop" and the "20201112_JW3 report.pdf"? – mason Jan 20 '21 at 00:11

2 Answers2

2

Use System.IO.Path.Combine to assemble paths with directory separators included.

Dim filename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                                      TextBox1.Text & " report.pdf")

The function takes a ParamArray argument, so you can provide an arbitrary number of strings to be combined into a single path.

I always try to use methods from System.IO.Path when I'm manipulating file paths. There aren't methods for absolutely everything I've needed to do, but most of the common operations are covered.

Craig
  • 2,248
  • 1
  • 19
  • 23
0

Change this line to (note the added slash):

Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + TextBox1.Text + " report.pdf"
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
  • As an informational aside, you're using VB so you may want to use `&` to concatenate rather than `+` (I do), but that's personal preference, although the MSDN docs also prefer you use `&`. Have a read: https://stackoverflow.com/questions/3006153/ampersand-vs-plus-for-concatenating-strings-in-vb-net – J. Scott Elblein Jan 20 '21 at 12:35
  • Another tip to handle potential bugs: make sure to test that everything is ok, just in case, with something like: `If String.IsNullOrEmpty(filename) = False` and `If My.Computer.FileSystem.FileExists(filename) = True ` where appropriate. – J. Scott Elblein Jan 20 '21 at 12:39