Advertisement
hungvb

How to Save Data to File and Read Data Back from File

Oct 20th, 2021
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.50 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.     Dim a As StreamReader
  5.     Dim b As String
  6.     Dim c As String = "C:\Program Files (x86)\temp\hungvb.txt"
  7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.         If Not File.Exists(c) Then
  9.             Dim d As FileStream
  10.             d = File.Create(c)
  11.             d.Close()
  12.         End If
  13.  
  14.         'read file
  15.         Try
  16.             ComboBox1.Items.Clear()
  17.             a = File.OpenText(c)
  18.             While a.Peek <> -1
  19.                 b = a.ReadLine()
  20.                 ComboBox1.Items.Add(b)
  21.             End While
  22.             a.Close()
  23.         Catch ex As Exception
  24.             MsgBox(ex.Message)
  25.         End Try
  26.  
  27.     End Sub
  28.  
  29.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  30.         If TextBox1.Text = Nothing Then
  31.             '  Me.Text = "Enter A Password To Be Saved"
  32.         Else
  33.             File.AppendAllText(c, TextBox1.Text & vbCrLf)
  34.             TextBox1.Text = ""
  35.             '  MsgBox("Saved Pass")
  36.             ReadFile()
  37.         End If
  38.     End Sub
  39.  
  40.     Private Sub ReadFile()
  41.         Try
  42.             ComboBox1.Items.Clear()
  43.             a = File.OpenText(c)
  44.             While a.Peek <> -1
  45.                 b = a.ReadLine()
  46.                 ComboBox1.Items.Add(b)
  47.             End While
  48.             a.Close()
  49.         Catch ex As Exception
  50.             MsgBox(ex.Message)
  51.         End Try
  52.     End Sub
  53. End Class
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement