Hey, i am working on a project that saves and load text files just so i can get the coding down for later projects. I am able to specify the location of the file to load eg. the Load File Dialog and the user specifies the location of the text file. What i needed help with is the the save file dialog, i want to be able to choose where the program saves too. This is the code i have for the save file dialog so far:

Private Sub SaveDoc()

        Dim NameOfDoc As String = txtAcc.Text & ".txt" 'Takes a substring of the title bar, the name of the file to be saved
        Dim FileNum As String = FreeFile()

        If NameOfDoc = "Template.txt" Then 'If it's a template, then run the SaveAsDoc() routined
            SaveAsDoc()
        Else    'Otherwise save all the neccessary data into the text file.

            FileOpen(FileNum, NameOfDoc, OpenMode.Output)
            PrintLine(FileNum, "Account: " & txtAcc.Text & vbCrLf & "Username: " & txtUser.Text & vbCrLf & "Password: " & txtCheese.Text)
            FileClose(FileNum)
            MsgBox("Details Saved")
        End If


    End Sub

    Private Sub SaveAsDoc()
        dlgSave.FileName = txtAcc.Text 'Sets the filename for saving

        If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Text = txtAcc.Text & " - " & txtUser.Text & ".txt" 'Sets the name of the form
            SaveDoc() 'Runs the routine SaveDoc()
        End If

    End Sub
Private Sub SaveAsDoc()
        dlgSave.FileName = txtAcc.Text 'Sets the filename for saving
        dlgSave.ShowDialog       
    End Sub 

   Private Sub dlgSave_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgSave.FileOk
            Me.Text = txtAcc.Text & " - " & txtUser.Text & ".txt" 'Sets the name of the form
            SaveDoc() 'Runs the routine SaveDoc()
    End Sub

im not that sure where your problem is exactlly. having trouble to save the file or just with the dialog? if its for the dialog then to code above helps. if its the saving of the file then i would suggest to use a textwriter/streamwriter for that.

My actual save code works but currently it only saves the files into the folder the program is in. What i wanted to do was choose what folder the file will save in eg. C:\Users\....

then use the dlgSave.FileOk event and put the code in there.
e.filename will include the path and filename as string

Try,

SaveFileDialog1.InitialDirectory = "c:\xyz"
        SaveFileDialog1.FileName = "p1.txt"
        SaveFileDialog1.ShowDialog()
This article has been dead for over six months. Start a new discussion instead.