hawk1ns Posted June 15, 2003 Posted June 15, 2003 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 Quote
*Experts* mutant Posted June 15, 2003 *Experts* Posted June 15, 2003 This will do it: Dim writer As New IO.StreamWriter("filename") writer.Write(TextBox1.text) writer.Flush() Quote
Leaders dynamic_sysop Posted June 15, 2003 Leaders Posted June 15, 2003 dont you think StreamReader / StreamWriter are just 1 of the best things since sliced bread:p it makes all the old vb stuff like #FreeFile look even worse than it was:-\ Quote
hawk1ns Posted June 16, 2003 Author Posted June 16, 2003 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 Quote
*Experts* mutant Posted June 16, 2003 *Experts* Posted June 16, 2003 What error are you getting? Quote
hawk1ns Posted June 16, 2003 Author Posted June 16, 2003 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'. Quote
Leaders dynamic_sysop Posted June 16, 2003 Leaders Posted June 16, 2003 you can either put this at the very top of your code page : Imports System.IO or you can do it like this : 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 Quote
Leaders dynamic_sysop Posted June 16, 2003 Leaders Posted June 16, 2003 another simple example is this : 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 Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 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 Quote
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 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 Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 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 Quote
Administrators PlausiblyDamp Posted June 17, 2003 Administrators Posted June 17, 2003 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 streamreader / writer / filestream is 1000's 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.