Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Save file dialog keeps saving into bin\debug folder and not in specified location.

Posted on 2008-10-03
2
Medium Priority
?
313 Views
Last Modified: 2012-08-13
Hello, i am trying to save the text from a textbox into a file using a save file dialog. I choose a folder however the file is not saved there but in the bin\debug folder. Below is the code i am using:

any help would be appreciated,

thanks.
Private Sub SaveTextToFileToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveTextToFileToolStripMenuItem.Click
        Dim saveFileDialog1 As New SaveFileDialog()
        Dim FileName As String
        Dim sw As System.IO.StreamWriter
        FileName = "Textfile"
 
 
        sw = New System.IO.StreamWriter(FileName)
        saveFileDialog1.FileName = FileName
        saveFileDialog1.Filter = "TXT files (*.txt)|*.TXT"
 
 
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            FileName = saveFileDialog1.FileName
 
           
            sw.Write(txtString.Text)
            sw.Close()
 
 
        End If
 
    End Sub
End Class

Open in new window

0
Comment
Question by:pythonV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 11

Assisted Solution

by:ladarling
ladarling earned 500 total points
ID: 22635184
You are kind of doing things backward...
When you open 'FileName' with the streamwrite, the current directory for your broject is 'bin/debug', so that is where the file is created.
You need to get the path to where the save should occur *first*, then do your write.
 

        Dim saveFileDialog1 As New SaveFileDialog()
        Dim FileName As String
        Dim sw As System.IO.StreamWriter
 
        saveFileDialog1.FileName = FileName
        saveFileDialog1.Filter = "TXT files (*.txt)|*.TXT"
        saveFileDialog1.ShowDialog()
 
        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            FileName = saveFileDialog1.FileName
            sw = New IO.StreamWriter(FileName)
            sw.Write(txtString.Text)
            sw.Close()
        End If
 
    End Sub

Open in new window

0
 
LVL 11

Accepted Solution

by:
ladarling earned 500 total points
ID: 22635257
Then again, this way is probably a little more reliable:
        Dim sfd As New SaveFileDialog
        With sfd
            .Filter = "TXT files (*.txt)|*.TXT"
            .FileName = "TextFile"
        End With
        If Not String.IsNullOrEmpty(sfd.FileName) And sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim sw As New IO.StreamWriter(sfd.FileName)
            sw.Write(txtString.text)
            sw.Close()
        End If

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question