Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » .NET Programming » Visual Basic .NET/2005/2008 » Problems about saving text into a file. Pls...


Problems about saving text into a file. Pls...Expand / Collapse
Author
Message
Posted 9/12/2007 12:31:27 AM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: 11/30/2007 1:44:01 AM
Posts: 23, Visits: 144
Hi everyone,

I'm using Visual Basic 2005.

What i am going to do is:

1) Whatever i typed into the textbox, once i click the button, it will automatic save into a file which is in .txt form
2)  Do not overwrite the file, just add into it.

I have read through a method of Saving a File from a book:

Imports System.IO
Public Class Form1
Dim myFileContents As String

Private
Sub SaveFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFile.Click

If
SaveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim myStreamWriter As New StreamWriter(SaveFileDialog1.FileName)
If Not (myStreamWriter Is Nothing) Then
myStreamWriter.Write(FileContents.Text)
myStreamWriter.Close()

End If
End If
End Sub
End
Class

  • I have tried the program above, but it cannot auto save in text form. Everytime i save a file, i need to type "abc.txt".
  • And it only can either save into another file or overwrite it.

Q1: How to let it auto save in a text file?
Q2: And is there any method which no need to rename the file and just let it ADD into the existing file (not overwrite)

Thank you



Regards
Wanxi
Post #23006
Posted 9/12/2007 12:04:11 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 1:10:14 PM
Posts: 1,502, Visits: 3,491
Welcome to A1vbcode Wanxi.

Well I can't speak for Visual Basic 2005 but in VB6 you can use

Open App.Path & "\abc.txtFor Append As #1

Print #1, FileContents.Text

Close #1

You will have to be careful that the text doesn't get to big because I think there is a limit an the filesize on the txt file.

Append will add text to an extisting file without overwriting it. If the file doesn't exist then it will create a new one. 

Does VB 2005 have a descent help? Do a search for Input, Output and Append.

Keith

http://www.martin2k.co.uk/forums/

I've been programming with VB for 12 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Post #23010
Posted 9/12/2007 10:58:35 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior Member

Group: Forum Members
Last Login: 11/30/2007 1:44:01 AM
Posts: 23, Visits: 144
Thank You the information from Anonymous and LadzOfSoul

Below are the 2 answers that i received by private message from them. I have study both of them, there is not much different at the last part, except for:

"myStreamWriter.Write(ans & TextBox1.Text)" By using a WriteLine for me will be better, bcos i'm going to receive a same data but different time.

Another things is "OpenFileDialog1" If you are not required to keep rename the file's name then you no need to put OpenFileDialog, just type the address at the Stream.


'ANSWER of Anonymous :
Imports System.IO
Public Class Ans1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ans As String 'ans is the File Contents
Dim myStreamReader As New StreamReader("c:\New Text.txt") 'Open & read from the location
If Not (myStreamReader Is Nothing) Then 'if myStreamReader is not Nothing? (means there is a StreamReader?)
ans = myStreamReader.ReadToEnd.ToString 'Read the New Text and put it into ans
myStreamReader.Close() 'After read must close
Dim myStreamWriter As New StreamWriter("c:\New Text.txt") 'Write to the location
myStreamWriter.WriteLine(ans & TextBox1.Text) 'Write the ans that i read from the file and add the words that i type into the TextBox1 in a new line
myStreamWriter.Close() 'After write also need to close
End If
End Sub
End
Class


'ANSWER of LadzOfSoul :
Imports System.IO
Public Class Ans2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ans As String
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim myStreamReader As New StreamReader(OpenFileDialog1.FileName)
If Not (myStreamReader Is Nothing) Then
ans = myStreamReader.ReadToEnd.ToString
myStreamReader.Close()
Dim myStreamWriter As New StreamWriter(OpenFileDialog1.FileName)
myStreamWriter.Write(ans & TextBox1.Text)
myStreamWriter.Close()
End If
End If
End Sub
End
Class

There are some problems that i wonder:

  • What is the meaning of "If Not (myStreamReader Is Nothing) Then"?
  • And for this >> "Dim myStreamReader As New StreamReader(OpenFileDialog1.FileName)" Ist "OpenFileDialog1.FileName" is a fixed name? Or what else i can put on it?

I never use myStreamReader/Writer before, so there is a bit difficult for me to understand a single program, by compare them make me realies how it works.. Thanks alot..

Regards
Wanxi

Post #23019
Posted 9/16/2007 7:23:04 PM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum God

Group: Forum Members
Last Login: Today @ 5:04:51 PM
Posts: 866, Visits: 5,185
This line of code:

If SaveFileDialog1.ShowDialog() = DialogResult.OK Then

You are calling the save dialog everytime you save the file. Somewhere  else in your code put:

        Dim FileName As String = String.Empty
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            FileName = SaveFileDialog1.FileName
        End If

and change to this:

        Dim myStreamWriter As New StreamWriter(FileName)


Wayne

I know enough to be dangerious.

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.

Post #23046
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Brian, Peter

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 8:19pm