从文本文件中检索数据 [英] retrieve data from text file

查看:65
本文介绍了从文本文件中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,


如何使用C#从文本文件访问用户名和密码.

Dear Friends,


How to access username and password from text file using C#.??

推荐答案

以下方法可用于将所有文本行写入文本文件并读取文本文件中所有文本行.
The following methods can be used to write all the lines of text to text file and to read all the lines of text from text file.
System.IO.File.WriteAllLines(fileName,new string[]{"username","password"});
string[] userData = System.IO.File.ReadAllLines(fileName);


在这里,我没有使用过用户名=,密码=.可以使用它,然后必须从字符串中提取相关部分.

但是,主要的一点是,如果将用户名(尤其是密码)原样写入文本文件,则任何机构都可以轻松访问,并且破坏了身份验证的整个目的.
SAKryukov 此处
对此方面进行了说明 以c#格式验证用户名和密码 [


Here, I have not used username =, password =. It can be used, then you have to extract the relevant portion from the string.

But, the major point is, if the username and in particular password is written to the text file as it is, any body can easily access and the whole purpose of authentication is defeated.
This aspect is explained by SAKryukov here
verify user name and password in c# form[^]


这不是在文本文件中存储用户名和密码的好方法.无论如何,您都可以喜欢以下内容


This is not a good way to store username and password in textfile. anyway you can do like following


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string username, password;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string filename = @"C:\Users\user\Desktop\Projects\CodeProject\textboxread\credentials.txt";
        // The textfile contains details
        //username=loginname
        //password=loginpassword

        var lines = File.ReadAllLines(filename);
        foreach (string line in lines)
        {
            if (line.Contains(@"username"))
            {
                string str = line; int index = str.IndexOf("="); 
                string newstr = str.Substring(index + 1);
                username = newstr.Trim();
            }
            if (line.Contains(@"password"))
            {
                string str = line; int index = str.IndexOf("="); 
                string newstr = str.Substring(index + 1);
                password = newstr.Trim();
            }

        }
        if((TextBox1.Text.Trim().Equals(username)) &&               (TextBox2.Text.Trim().Equals(password)))
        {
            //Login Code
        }
        else
        {
            Label1.Text = "Invalid Login";
        }

    }
}




这篇关于从文本文件中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆