how to set destination directory path to save file

Dani AI

Generated

This thread already shows the two common approaches: dialog-based selection (as noted by ) and directory picking then creating a file (as demonstrated). The code below expands those ideas into safer, reusable patterns: prefer Path.Combine instead of manual string concatenation, ensure the target directory exists before writing, and always handle/dispose streams and exceptions so writes don't leave locked files or unhandled crashes.

Imports System.IO

' Programmatic save to a known location (robust pattern)
Dim destDir As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyApp", "Exports")
If Not Directory.Exists(destDir) Then
    Directory.CreateDirectory(destDir)
End If

Dim destFile As String = Path.Combine(destDir, "output.txt")
Try
    File.WriteAllText(destFile, "Sample content")
Catch ex As UnauthorizedAccessException
    ' permission problem
Catch ex As PathTooLongException
    ' long-path on older runtimes
Catch ex As IOException
    ' locked file, disk error, etc.
End Try
' Interactive save with SaveFileDialog (sets a sensible start folder and ensures directory exists)
Dim dlg As New SaveFileDialog()
dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
dlg.FileName = "output.txt"
dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

If dlg.ShowDialog() = DialogResult.OK Then
    Dim folder As String = Path.GetDirectoryName(dlg.FileName)
    If Not Directory.Exists(folder) Then Directory.CreateDirectory(folder)
    File.WriteAllText(dlg.FileName, "Saved via dialog")
End If

Troubleshooting notes: avoid concatenating paths with "\"; use Path.Combine. When using File.Create, dispose the returned stream (Using ... End Using) so handles are closed—'s example created a file but did not show disposing. Check for invalid path/file name chars with Path.GetInvalidPathChars / Path.GetInvalidFileNameChars. Watch permissions and network share (UNC) access, and remember older runtimes enforce ~260-char path limits. The environment-variable/compilation question from is a separate topic and is best handled in a new thread.

Recommended Answers

All 4 Replies

Assuming you are using a SaveFileDialog, try .InitialDirectory = "yourpath"

how to set destination directory path to save file

If this doesn’t help you, please explain your question in more detail

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

        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Console.WriteLine("Selected FolderPath: " & fbd.SelectedPath)
            IO.File.Create(fbd.SelectedPath & "\Test.txt")
            Console.WriteLine(fbd.SelectedPath & "\Test.txt Created")
        End If

    End Sub

i have a question that
i have installed vb .net in some other folder other than c how can i set the path in environment variables to compile and run my programs....
m i clear. ??
plz do reply....

I suggest that you create a new thread for this question as it has nothing to do with this thread and probably won't get answered here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.