Results 1 to 6 of 6

Thread: A Quick Saving Question *Solved*

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    A Quick Saving Question *Solved*

    Hello! What I am trying to do is to have 1 button in a form save both a bitmap and a text file in the same folder with just one click. So what I wanted it to do was to open a FolderBrowserDialog for the user to select a folder to save them in. I get the MsgBox saying "done" at the end though, but I see no created files.
    Nothing is saved with this code:
    vb Code:
    1. Private Sub ButtonSave_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSave.Click
    2.         If MessageBox.Show("Would you like to save?", "Save As...", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
    3.             MessageBox.Show("Please choose a folder.", "Save As...", MessageBoxButtons.OK)
    4.             If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    5.                 Dim Map As New Bitmap(PanelFullMapToSave.Width, PanelFullMapToSave.Height)
    6.                 PanelFullMapToSave.DrawToBitmap(Map, New Rectangle(0, 0, PanelFullMapToSave.Width, PanelFullMapToSave.Height))
    7.                 Map.Save(FolderBrowserDialog1.SelectedPath & "bitmap.png", System.Drawing.Imaging.ImageFormat.Png)
    8.                 Dim SW As System.IO.StreamWriter = New System.IO.StreamWriter(FolderBrowserDialog1.SelectedPath & "data.txt")
    9.                 SW.WriteLine(Data)
    10.                 SW.Dispose()
    11.                 MsgBox("done")
    12.             End If
    13.         End If
    14.     End Sub

    HOWEVER I had previously used this code using a SaveFileDialog, and it worked (saved the bitmap in the location.) In this case I had only saved the bitmap.
    vb Code:
    1. Private Sub ButtonSave_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSave.Click
    2.         If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    3.             Dim Map As New Bitmap(PanelFullMapToSave.Width, PanelFullMapToSave.Height)
    4.             PanelFullMapToSave.DrawToBitmap(Map, New Rectangle(0, 0, PanelFullMapToSave.Width, PanelFullMapToSave.Height))
    5.             Map.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png)
    6.         End If
    7.     End Sub

    Did I miss something in the first block of code? Thanks.
    Last edited by NinjaNic; Aug 2nd, 2014 at 03:37 PM.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: A Quick Saving Question

    Debug the file path. It's wrong. You should always debug.

    Code:
    Dim path As String = IO.Path.Combine(fbd.SelectedPath, "image.jpeg")

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: A Quick Saving Question

    Solved! I forgot the back slash. Thanks for the help!

  4. #4

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: A Quick Saving Question

    Quote Originally Posted by NinjaNic View Post
    Solved! I forgot the back slash. Thanks for the help!
    NO!

    ident showed you the correct way, use IO.Path.Combine, otherwise if you add a backslash to your code and the user happens to select the root drive as the destination then your code will most likely fail since the file path would then contain two backslashes in it.

    EDIT: Although it may not always matter, under Win7 using IO.File this still works without error!
    IO.File.WriteAllText("M:\\\New folder\\OK.TXT", "Testing 1,2,3.")
    Last edited by Edgemeal; Aug 2nd, 2014 at 04:00 PM.

  6. #6

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: A Quick Saving Question *Solved*

    Thanks, I read his code wrong and put the & instead of a comma. Now I can remove the back slash and it works.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width