save the contents of a textbox

tennisfreak

Member
Joined
Sep 7, 2007
Messages
13
Location
El Salvador
Programming Experience
Beginner
hey... I want to save the contents of a textbox, or more textbox in a .txt file is this possible?

I would also like to know how to print the contents of a datagrid.

thanks for the help... :)
 
hey tennisfreak,

try this:

Dim fileExists As Boolean
fileExists = My.Computer.FileSystem.FileExists("C:\Test.txt")
If fileExists = True Then
My.Computer.FileSystem.WriteAllText("C:\Test.txt", "dude this is great!", True)
Else
My.Computer.FileSystem.WriteAllText("C:\Test.txt", String.Empty, False)

My.Computer.FileSystem.WriteAllText("C:\Test.txt", "I know i love it!", True)
End If

Hope this helps!

Alex
 
hey tf.

My.Computer.FileSystem.WriteAllText("C:\Test.txt", textbox1.text, True) would work.

regards

ales

Why would you use the 3 parameter constructor, passing in True meaning "append" ?
Infact.. Why would you use the Legacy VB function at all, when you can use the .NET native function:

Public Shared Sub WriteAllText(ByVal path As String, ByVal contents As String)
Member of: System.IO.File
Summary:
Creates a new file, writes the specified string array to the file, and then closes the file. If the target file already exists, it is overwritten.

Parameters:
contents: The string array to write to the file.
path: The file to write to.

Exceptions:
System.ArgumentNullException: path is null.
System.Security.SecurityException: The caller does not have the required permission.
System.NotSupportedException: path is in an invalid format.
System.ArgumentException: path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by System.IO.Path.InvalidPathChars.
System.IO.PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
System.IO.IOException: An I/O error occurred while opening the file.
System.UnauthorizedAccessException: path specified a file that is read-only.-or- This operation is not supported on the current platform.-or- path specified a directory.-or- The caller does not have the required permission.
System.IO.FileNotFoundException: The file specified in path was not found.
System.IO.DirectoryNotFoundException: The specified path is invalid (for example, it is on an unmapped drive).

So, tennis freak, jsut write:

System.IO.File.WriteAllText(pathTextBox.Text, contentTextBox.Text)
 
Using the My feature is recommended, it is not "legacy", it's the new thing and for now a VB only language feature (so cjard is just envious for now for simplicity C# doesn't have ;)). For example My.Computer.FileSystem.WriteAllText is a wrapper for common System.IO Streamwriter code. But since it is new in .Net 2.0 tennisfreak can't use it.
 
I'm now on VS2005, I found another code to do it:

Imports System.IO

Private Sub btnAppendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppendFile.Click
Dim stream_writer As StreamWriter
stream_writer = AppendText(txtFileName.Text)
stream_writer.WriteLine(txtText.Text)
stream_writer.Close()
End Sub
 
I'm now on VS2005, I found another code to do it:

Imports System.IO

Private Sub btnAppendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppendFile.Click
Dim stream_writer As StreamWriter
stream_writer = AppendText(txtFileName.Text)
stream_writer.WriteLine(txtText.Text)
stream_writer.Close()
End Sub


I still reckon:

VB.NET:
Private Sub btnAppendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAppendFile.Click
        System.IO.File.WriteAllText(txtFileName.Text, txtText.Text)
    End Sub

is simpler. Good for C# too! :)

/me tries to recall a single time he ever used the My namespace. Hmm.
 
/me tries to recall a single time he ever used the My namespace. Hmm.
Don't you use the settings and resources?? I find those very convenient.
 
Back
Top