2015-02-10 127 views
0

我正在尝试创建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

帖子相关代码在这里,而不是在其他网站上。 – crashmstr 2015-02-10 18:50:14

+0

您是否尝试在调试模式下逐行运行程序?什么时候确切地做事情不起作用? – Supersnake 2015-02-10 18:55:17

+0

该程序工作得很好,但是当登录成功时,它不会将用户名和密码写入.txt文件(但它应该) – Kresenko 2015-02-10 19:08:32

回答

0

首先请注意 - 绝不要在PC或任何其他地方向文件写入登录名和密码。

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

enter image description here

enter image description here

试试看......

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 
+0

谢谢大家的帮助:)我修好了 – Kresenko 2015-02-10 20:51:50