How do i save?!

  • 14 years ago
       Hello, i am new here (15) and need major help. As my first project i am making a notepad that makes batch files. How the heck do i program a save feature? Please Help!
  • 14 years ago

    I'm guessing you have a textbox that the user types the batch code into.  I'll use that in my example.  If not, you should still be able to get the idea.  I think using "System.IO.File.WriteAllText" will work for you.  You can also use a stream writer object as well but I'll leave that for you to explore.  The StreamWriter class is found in the "System.IO" namespace.  Take a look at this code and see if you can work it into your application.  If you have any trouble let me know.

            'This will over-write the file.  You may want to check
            'for it's existence and ask the user if they want to over-write it.
            IO.File.WriteAllText("c:\test.bat", TextBox1.Text)
            'To check for a files existance and ask if it's okay to over-write
            'you could use
            If IO.File.Exists("c:\test.bat") Then
                If MsgBox("This file already exists. Would you like to save anyway?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    IO.File.WriteAllText("c:\test.bat", TextBox1.Text)
                End If
            End If
        End Sub
  • 14 years ago
    Thank you, this makes some sense to me. But how do I make it so you can pick to save it as .bat or .txt. I don't know maybe i'm confusing myself. I'm thinking of a save as or save feature.
  • 14 years ago

    You could use the SaveFileDialog.  You should be able to find it in the tools menu.  Just drop it onto your form and your all set. The following code will demonstrate it.  You can customize the dialog of few ways.  I don't know everything about it but you can probably figure most things out by messing around with it.  The most important thing to change would be the filter which you can see in the code below.  I set it so the user can select text files, batch files, or all files.  It works just as your used to.  If the user selects Batch Files and types a name without the extension the extension will automatically be added.  No additional coding on your part.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Filter string format is "Description1|pattern1|Decription2|pattern2..."
            SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|Batch Files(*.bat)|*.bat|All Files (*.*)|*.*"
            'Displays the SaveFileDialog and saves the file if the user selects save
            If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'SaveFileDialog1.FileName will give you the complete file name
                'Code to save file can be written here
                'Just get the filename from the dialog
            End If
        End Sub
  • 14 years ago
    This is what I was looking for!!! Thank you very much! Batch Maker here I come!

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic