【问题标题】:Saving user input in VB在 VB 中保存用户输入
【发布时间】:2013-10-16 19:02:19
【问题描述】:

在我的 VB 应用程序开始时,我要求用户在两个文本框中输入他的电子邮件和密码。如果登录成功,我想将输入的电子邮件和密码作为 Login.txt 文件保存到相关目录中。我想覆盖文件中的所有旧信息。

我希望每次应用程序启动时都这样;它将读取这个 Login.txt 文件并在相同的两个文本框中显示电子邮件和密码,因此用户无需在每次登录时都输入它。

什么是正确的代码?

感谢您的帮助。

【问题讨论】:

  • 不要以纯文本形式保存密码。相反,请从您的服务器保存一个可撤销的令牌,例如 OAuth 2。
  • 如果您将所有登录信息保存为下次默认使用,为什么还要登录呢?你至少应该要求你的用户输入他的密码。
  • 转到项目 -> 属性 -> 设置。如果您没有将其设置为自动保存,则它只是My.Settings.Save
  • 安全级别 0 - 无密码,安全级别 0.1 - 密码保存在文件中。您必须决定什么是应用程序的安全性。听起来你不知道答案。

标签: vb.net text input


【解决方案1】:

首先,您需要遵循@Plutonix 评论步骤, 您可以创建两个新设置作为字符串类型并选择“用户”范围(不是应用程序),并确保将默认值保持为空。

因此,在您的代码中,您可以在每次需要时保存值:

My.Settings.Email = EmailTextBox.text
My.Settings.Pass  = PassTextBox.text

并加载值:

EmailTextBox.text = My.Settings.Email 
PassTextBox.text  = My.Settings.Pass  

【讨论】:

    【解决方案2】:

    我一直在使用这种方法,我认为它会对你有所帮助。 当您打开 VB 时,在独奏资源管理器“我的项目”中双击 它会在您需要单击设置选项卡的窗口侧面出现选项卡。它将提出一个表,其中一行将具有名称“设置”。在您的情况下,您想将其命名为“电子邮件”,然后按 Enter。将再次出现另一行,将名称从“设置”更改为“密码”,然后按 Enter。完成这部分后,这是编码部分:

    编码:

    Textbox1.text = my.settings.email

    Textbox2.text = my.settings.password

    我的.settings.save

    好的,这就是编码的方式

    您现在需要做的就是将上面的编码插入到您要单击的按钮的编码中以记住详细信息。 然后在您完成该步骤后单击调试或播放按钮或 (f5) 键。

    希望这对你有用,如果没有,我会编辑它

    谢谢

    【讨论】:

      【解决方案3】:

      希望以下参考资料能为您的问题提供解决方案

      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
              pth = "d:\user_login"
              If Directory.Exists(pth) = False Then
                  Directory.CreateDirectory(pth) 'create a folder in the path is no such folder is existing
              End If
          End Sub
      

      这将在第一次运行时在您的 D: 驱动器中创建一个目录

      Private Sub login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login.Click
              ' works like session veriables 
              uname = username.Text ' store user name to a public verible for future reference till the application restarted
              pwd = pasword.Text    ' store password to a public verible for future reference till the application restarted
              'save user name and password as small text file for future reference
              'Dim rtc As New RichTextBox
              rtc.Clear() ' clear the rtc content
              rtc.Text = "Username :" & username.Text & Chr(13) & "Password :" & pasword.Text 'create a text document and write values on it
              rtc.SaveFile(pth & "\" & Now.Month.ToString & Now.Day & "_" & Now.Hour.ToString & "_" & Now.Minute.ToString & ".txt") 'save the document in the above path with current date and time as name
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多