how to save textbox to a file ?

hawk1ns

Active member
Joined
Jun 10, 2003
Messages
37
hello , in an older version ( vb 5 ) i used this to save content of a textbox to a file...


nameoffile = "html\" & filename & ".htm"
Open nameoffile For Output As #2
Print #2, Text9.Text
Close #2

this worked just fine , but i cant seem to get it working on .net ?
any suggestions please...

Kind Regards
Carl
 
i tried that but something wrong...

Dim nameoffile : Dim savetext
nameoffile = "c:\affwiz\html\" & TextBox6.Text & ".htm"

Dim writer As New IO.StreamWriter(nameoffile)
writer.Write(filesave.Text)
writer.Flush()


Above is what i tried , i get an error under Writer ( Dim Writer )

also i need to save over 100 files and the name for each file will change every time , thats why i use the nameoffile string, have i used this correctly ?


Regards
Carl
 
C:\affwiz\DataForm1.vb(1062): Overload resolution failed because no accessible New can be called without a narrowing conversion:
Public Overloads Sub New(path As String): Argument matching parameter path narrows from System.Object to String.
Public Overloads Sub New(stream As System.IO.Stream): Argument matching parameter stream narrows from System.Object to System.IO.Stream.
 
you can either put this at the very top of your code page :
Imports System.IO

or you can do it like this :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim writer As New System.IO.StreamWriter("C:\test.txt")

        writer.Write("test") /// write some text to save in our test.txt
        writer.Flush() /// empty the writer.
        writer.Close() /// close it down

    End Sub
 
another simple example is this :
Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sw As StreamWriter = New StreamWriter(New FileStream("C:\test.ini", FileMode.Create))
        sw.WriteLine(TextBox1.Text)
        sw.Close()
    End Sub
 
thanks , but...

yes the solution works if i leave the line at :

Dim writer As New System.IO.StreamWriter("C:\test.txt")

this creates a new file at that location called test.txt

i need to create a veriable like..

nameoffile = "c:\affwiz\html\" & TextBox6.Text & ".htm"

and then use :

Dim writer As New System.IO.StreamWriter (nameoffile)

but when i do this i get a blue line under the write ?

so how can i change where it says C:\test.txt to my nameoffile veriable ?

thankyou for your help..

Regards
Carl
 
Code:
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim MyFile As String = "C:\" & TextBox2.Text & ".txt"
        //// make the file name / location to a string.
        WriteFile(MyFile) /// write the info to the new file.

    End Sub

    Public Function WriteFile(ByVal FileName As String)
        Dim FileToStream As StreamWriter = New StreamWriter(New FileStream(FileName, FileMode.Create))
        FileToStream.WriteLine("some text in to a new file!")
        FileToStream.Flush()
        FileToStream.Close()
    End Function
 
thanks for all the help ...
right i have now done it so that it works just the way i wanted it to but i used a different method....

Dim nameoffile : Dim savetext
nameoffile = "c:\affwiz\html\" & TextBox6.Text & ".htm"
savetext = filesave.Text
FileOpen(1, nameoffile, OpenMode.Output)
Print(1, savetext)
FileClose(1)

Are there any benifits to using sugested methods?

Regards
Carl
 
You really should move away from the old style VB methods of FileOpen, Print etc.

System.IO provides much more functionality with Streams, StreamReaders and StreamWriters as well as giving better code compatability with other .Net languages
 
streamreader / writer / filestream is 1000s of times better than the "old" vb methods of handling textfiles , it looks a lot tidier also and you can write / read with a lot less code + have more control over whats happening.
 
Back
Top