Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi everyone,

I am new to VB.net, and have only played around with VB6 as a hobby. I know this is not the best way to write and read from a file, but this is what I used in the past, and it worked great for me.

I am trying to write some variables to a simple text file, and then be able to use load them again at a later time. In VB 6 I used:

 

for saving (writing):

Open App.Path & "\file.txt" For Output As #1

Write #1, String1, String2, Integer1

Close #1

 

for loading (reading):

Open App.Path & "\file.txt" For Input As #1

Input #1, String1, String2, Integer1

Close #1

 

This wrote the three variables to a simple text file found in the same location as the startup path of the program. How can I recreate this same simple code for a VB.Net windows application? If possible, please let me know the easiest way to do this, even if it is not the correct or best method possible. Thanks!!

 

Michael

Posted (edited)
Public Class frmReadWriteTest
   Inherits System.Windows.Forms.Form

   Dim Save1, Save2, Save3 As String
Windows Form Designer generated code


   Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
       
       Save1 = txtOne.Text
       Save2 = txtTwo.Text
       Save3 = txtThree.Text

       FileOpen(1, "test.txt", OpenMode.Output)
       WriteLine(1, Save1, Save2, Save3)
       FileClose(1)
   End Sub

   Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click

       FileOpen(1, "test.txt", OpenMode.Input)
       Input(1, Save1)
       Input(1, Save2)
       Input(1, Save3)
       FileClose(1)

       txtFour.Text = Save1
       txtFive.Text = Save2
       txtSix.Text = Save3
   End Sub


End Class

Edited by divil
  • Leaders
Posted
I'm not sure what your point was behind posting your code. I asked if you'd searched the forum yet. There are a number of good examples for doing this the .NET-way posted.
--tim
Posted

arbitrary

 

I just wrote an arbitrary number of lines to my text file. Now, how can I read them in? I know what each line will contain, but I dont know how many lines. Is there some sort of a peek function to see if there are any more lines available to read?

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