Results 1 to 6 of 6

Thread: [RESOLVED] SaveFileDialog OverwritePrompt not overwritng

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    3

    Resolved [RESOLVED] SaveFileDialog OverwritePrompt not overwritng

    Hi, to all forum members, I'm new here and relatively new to VB.net. I am writing a small app to do some calculations then on the push of a save button save the formatted text in a .txt file. the problem I am having is the SaveFileDialog OverwritePrompt is not overwriting the .txt file but adding the text to the end of the previously saved text. This is probably an easy fix for you guys but I've been working on it for two days.

    Code:
        Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            Dim path As String
            Dim recipeTitle As String
    
            okPressed = False 'Public variable
            cancelPressed = False 'Public variable
    
            Dim inputForm As New frmInput 'new istance of frmInput you have to do this
            inputForm.ShowDialog() 'Show the input form as a model type
    
            'Set the save dialog properties
            With SaveFileDialog1
                .DefaultExt = "txt"
                .Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
                .FilterIndex = 1
                .OverwritePrompt = True
                .Title = "Save File Dialog"
            End With
    
            If okPressed = True And cancelPressed = False Then 'from the frmInput window
                If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Try
                        recipeTitle = title
                        'save the file path and name
                        path = SaveFileDialog1.FileName 'Assign the path and filename for the file to the variable path
    
                        'If Windows.Forms.SaveFileDialog.DialogResult.OK Then
                        'My.Computer.FileSystem.DeleteFile(path)
                        'End If
    
                        SaveFile(recipeTitle, path)
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, My.Application.Info.Title,
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            End If
        End Sub
        Public Sub SaveFile(ByVal thetitle As String, ByVal path As String)
    
            'This Writes to notepad and saves it to the path defined by the save dialog
            Dim file As System.IO.StreamWriter ' write to file
    
            file = My.Computer.FileSystem.OpenTextFileWriter(path, True) ' Give the path to the computer and check if the path exists
            ' Write to notepad
            file.WriteLine(thetitle)
            file.Write(vbCrLf)
            file.Write(meatBlock)
            file.WriteLine("g Meat Weight" & vbCrLf)
            file.WriteLine("Brine Ingredient Weights:")
            If waterWeight > 0 Then
                file.Write(waterWeight)
                file.WriteLine("g Water Weight")
            End If
            If answerNitriteweight > 0 Then
                file.Write(answerNitriteweight)
                file.WriteLine("g Nitrite Weight")
            End If
            If answerSalpetreWeight > 0 Then
                file.Write(answerSalpetreWeight)
                file.WriteLine("g Saltpetre Weight")
            End If
            If answerSaltWeight > 0 Then
                file.Write(answerSaltWeight)
                file.WriteLine("g Salt Weight")
            End If
            If answerSugarWeight > 0 Then
                file.Write(answerSugarWeight)
                file.WriteLine("g Sugar Weight")
            End If
            file.Write(vbCrLf & totalWeight)
            file.WriteLine("g Total Brine Weight" & vbCrLf)
            file.Write("Brine Concentration ")
            file.Write(answerBrineConcentration)
            file.Write("%")
    
            file.Close() 'Close notepad
    
        End Sub
    Any help would be much appreciated.

  2. #2
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: SaveFileDialog OverwritePrompt not overwritng

    Hi OldHead , Welcome to VB Forum.

    From one old head to another... I personally, wouldn't bother with overwrite, some of the younger heads in here (most) would say I still use 'old hat' coding, probably true, but I still remember when the B in BASIC stood for Beginners !

    I'd just write the file to disc as though where were no previous file. You could of course delete the previous file prior to writing the new one. To be frank, I've never heard of overwrite but I have a great many applications which write a new file each time they're used as well as those which append to a previous one, including restricting the number of entries to the last (say) twenty records.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: SaveFileDialog OverwritePrompt not overwritng

    You misunderstand what that property does. It doesn't overwrite anything. It just determines whether the dialogue will prompt the user to overwrite if they select an existing file. It's still up to you to write the code that saves the file and, in the code you have written, you have explicitly told the StreamWriter to append to the file if it exists. What do you think the True does here:
    vb.net Code:
    1. file = My.Computer.FileSystem.OpenTextFileWriter(path, True)
    If you don't want to append to an existing file then don't tell your StreamWriter to append. This is an example of why you should ALWAYS read the relevant documentation.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: SaveFileDialog OverwritePrompt not overwritng

    Quote Originally Posted by Poppa Mintin View Post
    I'd just write the file to disc as though where were no previous file. You could of course delete the previous file prior to writing the new one. To be frank, I've never heard of overwrite....
    I would say that it is in very poor taste for an application to overwrite a file without first warning the user. Windows users expect this and you're likely to induce an armed rebellion if your application doesn't adhere to this practice.

    The only circumstance where overwriting a file without warning is acceptable is if the file in question is the one being edited at the moment by the application. But if it's a new document and you attempt a save using a file name of a file that already exists, you better warn your users.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    3

    Re: SaveFileDialog OverwritePrompt not overwritng

    Hi Poppa Mintin thanks for your reply, Yes I first learnt Basic on the ZX spectrum I also learnt visual basic, but I didn't keep it up, now I'm trying to learn Visual Basic .net to keep my mind active. Anyway I digress I do write the file to disc but if I try to overwrite the same filename it doesn't overwrite but just tacks the text onto the end of the text of the old file.

    Here is the dialog that's not working as I would like, if I can find a way to see what button is being pressed I could delete the file through code.

    Attachment 186893

    Hi jmcilhinney, thanks for the reply, I have been on msdn and not found an answer. I probably have misunderstood so if you could explain and and help it would be of great help.
    Cheers OldHead.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    3

    Re: SaveFileDialog OverwritePrompt not overwritng

    Ok with a bit of prompting by jmcilhinney I went back on msdn and found the answer ie.

    from
    Code:
    file = My.Computer.FileSystem.OpenTextFileWriter(path, True)
    to
    Code:
    file = My.Computer.FileSystem.OpenTextFileWriter(path, False)
    Thanks All.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width