Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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

Posted

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'.

  • Leaders
Posted

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

  • Leaders
Posted

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

Posted

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

  • Leaders
Posted
   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

Posted

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

  • Administrators
Posted

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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...