写入现有文本文件

Write to existing text file

我正在尝试创建一个 SignUpForm。当程序启动时,它会创建一个新文件,其中将在一侧写入用户名,在另一侧写入密码。这是我到目前为止所做的,但它不是一遍又一遍地写。请帮忙。谢谢!

Imports System.IO
Imports System.IO.File

Public Class SignUpForm

    Dim cnt As Integer
    Dim g As Integer
    Dim fileMembers As New System.IO.StreamWriter("Members.txt")

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            TextBox2.PasswordChar = ""
            TextBox3.PasswordChar = ""
        Else
            TextBox2.PasswordChar = "*"
            TextBox3.PasswordChar = "*"
        End If
    End Sub

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

        If TextBox2.Text = TextBox3.Text And TextBox4.Text = recaptcha.Text Then
            test()
            MsgBox("Passwords Match, Logged It!")
            Me.Close()
        End If
        If TextBox2.Text <> TextBox3.Text Then
            MsgBox("Passwords Do Not Match")
            TextBox2.Text = ""
            TextBox3.Text = ""
        End If
        If TextBox4.Text <> recaptcha.Text Then
            MsgBox("The verification code isn't valid")
            TextBox4.Text = ""
        End If

    End Sub

    Private Sub SignUpForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cnt = 0
        g = 1
    End Sub

    Private Sub Main()
        fileMembers.WriteLine("Username" + " : " + "Password")
        fileMembers.WriteLine("===================")
        fileMembers.Close()
    End Sub

    Private Sub test()
        If Not File.Exists("Members.txt") Then
            Using sw As StreamWriter = File.CreateText("Members.txt")
                sw.WriteLine("")
            End Using
        End If

        Using sw As StreamWriter = File.AppendText("Members.txt")
            sw.WriteLine(TextBox1.Text + " : " + TextBox2.Text)
        End Using

        Using sr As StreamReader = File.OpenText("Members.txt")
            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
        End Using
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        cnt = cnt + 1
        Label4.Text = Str(cnt)
    End Sub

End Class

(我的问题是登录成功后应该写用户名和密码然后关闭程序。程序ether关闭不写或者报错说无法访问文件,因为它正在使用中)

首先请注意 - 切勿将登录名和密码写入 PC 或其他设备上的文件。

尽管如此,我还是为你的问题做了一个快速而肮脏的编码。所以请阅读代码并尝试从那里学习。当然,许多其他解决方案也是可能的。你必须清理你的代码,例如'TextBox2' => Pwd , 'TextBox3' => PwdRepeated 为了更好的阅读。快乐编码...

试一试...

   Imports System.IO
   Imports System.IO.File


   Public Class Form1

     Dim cnt As Integer
     Dim g As Integer

     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
       If CheckBox1.Checked Then
         TextBox2.PasswordChar = ""
         TextBox3.PasswordChar = ""
       Else
         TextBox2.PasswordChar = "*"
         TextBox3.PasswordChar = "*"
       End If
     End Sub

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

       If TextBox2.Text = TextBox3.Text And TextBox4.Text = lblrecaptcha.Text Then
         test()
         MsgBox("Passwords Match, Logged It!")
         Me.Close()
       End If
       If TextBox2.Text <> TextBox3.Text Then
         MsgBox("Passwords Do Not Match")
         TextBox2.Text = ""
         TextBox3.Text = ""
       End If
       If TextBox4.Text <> lblrecaptcha.Text Then
         MsgBox("The verification code isn't valid")
         TextBox4.Text = ""
       End If

     End Sub

     Private Sub SignUpForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       cnt = 0
       g = 1

       '--- open or create the file ----------
       Dim fs As FileStream = New FileStream(My.Computer.FileSystem.CombinePath(Application.StartupPath.ToString, "members.txt"), FileMode.OpenOrCreate, FileAccess.ReadWrite)
       Dim s As StreamWriter = New StreamWriter(fs)
       s.WriteLine("Username" + " : " + "Password")
       s.WriteLine("===================")
       s.Close()
       s.Close()
       fs.Close()

       cnt = cnt + 1
       Label4.Text = "DE-" + Trim(Str(cnt))
       lblrecaptcha.Text = "DE-" + Trim(Str(cnt))

     End Sub

     Private Sub test()
       '--- log it ----------
       Dim fs1 As FileStream = New FileStream(My.Computer.FileSystem.CombinePath(Application.StartupPath.ToString, "members.txt"), FileMode.Append, FileAccess.Write)
       Dim s1 As StreamWriter = New StreamWriter(fs1)


       s1.WriteLine(TextBox1.Text + " : " + TextBox2.Text)
       s1.WriteLine(DateTime.Now.ToLongTimeString() + TextBox1.Text + " : " + TextBox2.Text)
       s1.Close()
       fs1.Close()

       Dim sOut As String
       sOut = My.Computer.FileSystem.ReadAllText("Members.txt")
       Console.WriteLine(sOut)
     End Sub

   End Class