Save into Debug Folder

Wilfred Eghenedji 326 Reputation points
2022-04-08T12:42:08.337+00:00

THE CODE:

 Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
                SaveFileDialog1.ShowDialog()
                                If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName) Then
                    Dim ask As MsgBoxResult
                    ask = MsgBox("File already exists, would you like to replace it?", MsgBoxStyle.YesNo, "File Exists")
                                       If ask = MsgBoxResult.No Then
                        SaveFileDialog1.ShowDialog()
                                           ElseIf ask = MsgBoxResult.Yes Then
                        My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
                    End If
              Else
                    Try
                        My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, False)
                    Catch ex As Exception
                    End Try
                End If
 End Sub

THE PROBLEM

I have the above code. I want it to save into debug folder but all to no avail. I have tried various options but keeps opening 'my document' folder as save destination. I would like it to write the text to a file and save directly into the debug folder without showing openfiledialog.

ATTEMPTS:

  1. IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text).
  2. IO.File.WriteAllText("Application.StartupPath")

These makes no expected effect.

Thank you very much!

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,141 Reputation points
    2022-04-08T16:44:26.307+00:00

    Hi
    OK, trying to simplify things.
    Here is a single function that will create a unique path/filename each time it is called.

    Each time the User clicks the menustrip Save item, the function will supply a unique path/filename to the IO.File.Write ............ method used to save the contents of TextBox1.

        Function FinalPath() As String
            Dim path As String =
        IO.Path.GetFileNameWithoutExtension(BaseFileName) & My.Settings.Counter.ToString & IO.Path.GetExtension(BaseFileName)
            My.Settings.Counter += 1
            Return IO.Path.Combine(Application.StartupPath, path)
        End Function
        Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
            IO.File.WriteAllText(FinalPath, TextBox1.Text)
        End Sub
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2022-04-08T12:59:44.383+00:00

    Hi

    If I understand correctly, then you can build variations of below code. The components of the final 'myPath' can easily be made to be composed from direct hard coded string(s), or from TextBoxes etc. The main thing is that using the IO.Path.Combine will ensure a valid path is used. NOTE: doing things like this makes the save location 'follow' the exe. Wherever the application is started from is where the file would be saved.

     Dim myPath As String = IO.Path.Combine(Application.StartupPath, "myFileName.txt")
    
     IO.File.WriteAllText(myPath, TextBox1.text)
    

  2. LesHay 7,141 Reputation points
    2022-04-08T13:55:17.207+00:00

    Hi

    There are many ways to deal with that.

    One way: Use a variation of Now.ToString("ddd, dd MMM yyyy") and incorporate into file name.

    Another way: Keep a counter variable (say in My.Settings) and append to filename.

    Another way: Use a TextBox with a default name portion and allow user to add/edit to that and use as filename.

    If you need an example, just post back and ask.


  3. LesHay 7,141 Reputation points
    2022-04-08T14:50:10.513+00:00

    Hi
    Second attempt to post an editted answer.

    Hi
    Example - no problem. Here are 3 variations. This is a stand alone example, and needs a new Project with default Form1 containing a ListBox1 and Buttons 1,2 and 3. Also needs a My.Settings.Counter as an Integer value - initialized to 1. Depending on your needs, the easiest one is the My.Settings.Counter one. The BaseFileName is used in all generated file names in one way or another.

    ' Form1 with ListBox1,
    ' Buttons 1, 2 1nd 3
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim BaseFileName As String = "Accounts.txt"
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim part1 As String = Now.Ticks.ToString & " " & IO.Path.GetFileName(BaseFileName)
    
            ListBox1.Items.Add("Using TICKS")
            ListBox1.Items.Add(FinalPath(part1))
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    
            Dim part1 As String = IO.Path.GetFileNameWithoutExtension(BaseFileName)
            Dim part2 As String = IO.Path.GetExtension(BaseFileName)
    
            ListBox1.Items.Add("Using LARGE DATE STRING")
            ListBox1.Items.Add(FinalPath(part1 & Now.ToString(" yyyy MM dd HH-mm-ss-ffff") & part2))
    
        End Sub
    
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    
            ' this is probably the simplest
            ' assuming a My.Settings.Counter as an Integer has been created.
    
            Dim part1 As String = IO.Path.GetFileNameWithoutExtension(BaseFileName)
            Dim part2 As String = IO.Path.GetExtension(BaseFileName)
    
            ListBox1.Items.Add("Using simple My.Settings.Counter")
            ListBox1.Items.Add(FinalPath(part1 & My.Settings.Counter.ToString & part2))
    
            ' will work over application start/end
            My.Settings.Counter += 1
    
        End Sub
    
        Function FinalPath(FinalFileName As String) As String
            ' make final path
            Return IO.Path.Combine(Application.StartupPath, FinalFileName)
        End Function
    End Class
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.