Solved

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

Posted on 2008-10-03
2
282 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
  • 2
2 Comments
 
LVL 11

Assisted Solution

by:ladarling
ladarling earned 125 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 125 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now