我正在尝试创建一个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
(我的问题是,登录成功后,应该写入用户名和密码,然后关闭程序。程序以太网关闭,不写或发出错误说不能访问该文件,因为它正在使用中)
答案 0 :(得分:0)
首先请注意 - 永远不要在PC或其他任何地方写入文件的登录名和密码。
然而,我对你的问题进行了快速而肮脏的编码。所以请阅读代码并尝试从那里学习。当然,许多其他解决方案是可能的。您必须清理代码,例如'TextBox2'=&gt; Pwd,'TextBox3'=&gt; Pwd重复阅读。快乐的编码...
试一试......
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