Write text to text file without savefiledialog

DukeMeister44

Member
Joined
Sep 14, 2020
Messages
6
Programming Experience
1-3
Hi, I haven't posted here in a while.

I'm trying to figure out how to write a text file without a file save dialog, with a progress bar in a separate form (or status bar) indicating the file is being overwritten/modified and has to check if the file exists prior to writing the file to a specific directory in my advanced text editor called Pentapad.

Here is my code for saving a text file, works great but I want to modify the method of writing the text file without the dialog prompt.



Save file function:
If RichTextBox1.Modified = False Then
            MessageBox.Show("Cannot save a blank file, open a file or create one.", "Action can't be performed" & " - " & Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            RichTextBox1.Modified = True
            Dim SaveFile_result As DialogResult = MessageBox.Show("The file or document has been modified, Would you like to save it?", "Save Modified Document?", MessageBoxButtons.YesNo, MessageBoxIcon.Hand)
            If SaveFile_result = DialogResult.Yes Then
                Try
                    Dim sfd As New SaveFileDialog
                    sfd.Title = "Browse..."
                    sfd.AutoUpgradeEnabled = False
                    sfd.OverwritePrompt = True
                    sfd.FileName = "New Document.txt"
                    sfd.InitialDirectory = "C:\Users\" & Environment.UserName & "\Documents" 'goes the users documents folder
                    sfd.DefaultExt = "Text Files (*.txt*)|*.txt*|Rich Text File (*.rtf*)|*.rtf*|Windows Resource File (*.rc)|*.rc|Automatic Hot-Key Script (*.ahk)|*.ahk|C/C++ Header (*.h)|*.h|C/C++ Source File (*.c)|*.c|C/C++ Main Source File (*.cpp)|*.cpp|C# Source File (*.cs)|*.cs|Configuration File (*.cfg)|*.cfg|Definitions File (*.def)|*.def|Group Cache (*.grpcache)|*.grpcache|Windows Batch Script (*.bat)|*.bat|Visual Basic Script (*.vbs)|*.vbs|Visual Basic for Applications (*.vba)|*.vba|UNIX/Linux Bourne Again Shell Files (*.bash_profile), (*.profile), (*.bashrc), (*.bash), (*.sh), (*.csh)|*.bash_profile, *.profile, *.bashrc, *.bash, *.sh, *.csh|UNIX/Linux Desktop Shortcut (*.desktop)|*.desktop|Markdown Document (*.md)|*.md|All Files (*.*)|*.*"
                    sfd.Filter = "Text Files (*.txt*)|*.txt*|Rich Text File (*.rtf*)|*.rtf*|Windows Resource File (*.rc)|*.rc|Automatic Hot-Key Script (*.ahk)|*.ahk|C/C++ Header (*.h)|*.h|C/C++ Source File (*.c)|*.c|C/C++ Main Source File (*.cpp)|*.cpp|C# Source File (*.cs)|*.cs|Configuration File (*.cfg)|*.cfg|Definitions File (*.def)|*.def|Group Cache (*.grpcache)|*.grpcache|Windows Batch Script (*.bat)|*.bat|Visual Basic Script (*.vbs)|*.vbs|Visual Basic for Applications (*.vba)|*.vba|UNIX/Linux Bourne Again Shell Files (*.bash_profile), (*.profile), (*.bashrc), (*.bash), (*.sh), (*.csh)|*.bash_profile, *.profile, *.bashrc, *.bash, *.sh, *.csh|UNIX/Linux Desktop Shortcut (*.desktop)|*.desktop|Markdown Document (*.md)|*.md|All Files (*.*)|*.*"
                    sfd.SupportMultiDottedExtensions = False
                    If sfd.ShowDialog = DialogResult.OK Then
                        System.IO.File.WriteAllText(sfd.FileName, RichTextBox1.Text)
                        Me.Text = (sfd.FileName & " - " & Application.ProductName)
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.None) ' will output an error message if something in the application has created the error :}
                End Try
            End If
        End If
 
I honestly don't think that you have thought this through. The only thing the the dialogue does is provide the file path selected by the user. If you don't want to use that file path then don't. Just put something else where you are currently using the path from the dialogue:
VB.NET:
System.IO.File.WriteAllText(putSomethingElseHereThatContainsThePathYouWant, RichTextBox1.Text)
 
On a different note, you are currently giving the user the option of saving to, amongst others, plain text or RTF, but you then just save the Text of the RichTextBox regardless. How does it make sense to let the user select RTF and then not save the Rtf of the control? Is there even any formatting in the RichTextBox? If not then why are you letting them select an RTF file in the first place?
 

Latest posts

Back
Top