卡住试图从文本文件输入字符串

Posted

技术标签:

【中文标题】卡住试图从文本文件输入字符串【英文标题】:Stuck trying to input into string from text file 【发布时间】:2016-04-12 02:21:33 【问题描述】:

我正在创建一个程序,它允许您解析用户名和密码的文本文件,然后针对数据库运行它们以返回他们的名字、姓氏和部门名称。我已经让程序逐行解析文本文件,并创建了 if else 语句来检查该行是否包含“[email]”或“[password]”。我想要做的是,如果该行包含其中之一,则将整行复制到我可以使用的字符串中。 这是我目前的代码,我承认这不是最好的,因为我还在学习。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Windows.Forms;

namespace trevorsenior

    public partial class Form1 : Form
    
        SqlConnection connection;
        string connectionString;
        public Form1()
        
            InitializeComponent();
        connectionString = ConfigurationManager.ConnectionStrings["trevorsenior.Properties.Settings.trevordatabaseConnectionString"].ConnectionString;
    

    private void Form1_Load(object sender, EventArgs e)
    

    
    private void parsetextfile()
    
        string filepath = string.Format(textBox1.Text);
        int count = 0;
        int sum = 0;
        foreach (var line in File.ReadAllLines(filepath))
        
            if (line.Contains("[email]")||line.Contains("[password]"))
            
                string email2="msue0001";
                string password2;
                password2 = "msuepassword";
                populateusername(email2, password2);
                count++;
            
            else
            
                sum++;
            
        
        if (sum>0&&count==0)
        
            MessageBox.Show("There are no usernames or passwords in this file");
        
    
    private void populateusername(string email2, string password2)
    
        string usernamein = email2;
        string passwordin = password2;
        string query = string.Format("SELECT * FROM [Table] WHERE username='" + usernamein + "' AND password='" + passwordin + "'");
        using (connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
        
            DataTable userstable = new DataTable();
            adapter.Fill(userstable);
            lister.DisplayMember = "firstname";
            lister.ValueMember = "userId";
            lister.DataSource = userstable;
        
        using (connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
        
            DataTable userstable = new DataTable();
            adapter.Fill(userstable);
            lister2.DisplayMember = "lastname";
            lister2.ValueMember = "userId";
            lister2.DataSource = userstable;
        
        using (connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
        
            DataTable userstable = new DataTable();
            adapter.Fill(userstable);
            lister3.DisplayMember = "deptname";
            lister3.ValueMember = "userId";
            lister3.DataSource = userstable;
        
    

    private void lister_SelectedIndexChanged(object sender, EventArgs e)
    

    

    private void button1_Click(object sender, EventArgs e)
    
        //populateusername();
        parsetextfile();
    

    private void browse_Click(object sender, EventArgs e)
    
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Open Phishing Textfile";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "TXT files| *.txt";
        fdlg.FilterIndex = 2;
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        
            textBox1.Text = fdlg.FileName;
        
    

这是我试图解析的文本文件:

Array
(

[lsd] => AVp4gWh_

[display] => 

[enable_profile_selector] => 

[isprivate] => 

[legacy_return] => 1

[profile_selector_ids] => 

[skip_api_login] => 

[signed_next] => 

[trynum] => 1

[timezone] => 240

[lgndim] => eyJ3IjoxMDI0LCJoIjo3NjgsImF3IjoxMDI0LCJhaCI6NzQxLCJjIjoyNH0=

[lgnrnd] => 170514_OoLu

[lgnjs] => 1460333318

[email] => jsmith0001

[pass] => jpassword

[persistent] => 

[default_persistent] => 1

[qsstamp] => W1tbMCwxNiwyOCwzMSw4MSw4OCw5MSw5NiwxMDAsMTUzLDE3MCwxOTAsMTk1LDIxNywyNDIsMjQ3LDI4OCwzMDgsMzE1LDMyNSwzMzUsMzY0LDM4MiwzODQsMzg1LDQxNSw0MzUsNDQ2LDQ2NSw0NzUsNDkzLDUwMSw1MjcsNTM3LDU0Miw1NTgsNTYxLDU3NSw1ODMsNjM0LDY0Myw3MzFdXSwiQVpsSTJqeGJ3Ul9RLXZzSEtYNk5EVE80LVNOdUhMOE1PbzdUeDJ2MWV6THc0dE11Wk5NOFNSMzEtZUJ2dnUya0tlNGpKdm9sVENmV0ZXRUx0YmtKZmlzSHRudUdMd201MFkwNm5oMFplb295OXZBa25JXzJZVnNxZDlZTE5SMUhEeVhtQkJyaXF2VlFaWElTNlVfZ3lwRlVnX2huSFhXWXlFZEQ5YUZabTMxaHg5NDBqOWE1d0d0TTFFQlNLQ0NTM3lYVVlSQl9rTjVLN0ZqOFdRUmxPa3hQazJoMC1mNkpyX0FrdXBSZzB5YmpOQSJd

)


Array
(

[lsd] => AVp4gWh_

[display] => 

[enable_profile_selector] => 

[isprivate] => 

[legacy_return] => 1

[profile_selector_ids] => 

[skip_api_login] => 

[signed_next] => 

[trynum] => 1

[timezone] => 240

[lgndim] => eyJ3IjoxMDI0LCJoIjo3NjgsImF3IjoxMDI0LCJhaCI6NzQxLCJjIjoyNH0=

[lgnrnd] => 170514_OoLu

[lgnjs] => 1460333337

[email] => msue0001

[pass] => mspassword

[persistent] => 

[default_persistent] => 1

[qsstamp] => W1tbMCwxNiwyOCwzMSw4MSw4OCw5MSw5NiwxMDAsMTUzLDE3MCwxOTAsMTk1LDIxNywyNDIsMjQ3LDI4OCwzMDgsMzE1LDMyNSwzMzUsMzY0LDM4MiwzODQsMzg1LDQxNSw0MzUsNDQ2LDQ2NSw0NzUsNDkzLDUwMSw1MjcsNTM3LDU0Miw1NTgsNTYxLDU3NSw1ODMsNjM0LDY0Myw3MzFdXSwiQVpsSTJqeGJ3Ul9RLXZzSEtYNk5EVE80LVNOdUhMOE1PbzdUeDJ2MWV6THc0dE11Wk5NOFNSMzEtZUJ2dnUya0tlNGpKdm9sVENmV0ZXRUx0YmtKZmlzSHRudUdMd201MFkwNm5oMFplb295OXZBa25JXzJZVnNxZDlZTE5SMUhEeVhtQkJyaXF2VlFaWElTNlVfZ3lwRlVnX2huSFhXWXlFZEQ5YUZabTMxaHg5NDBqOWE1d0d0TTFFQlNLQ0NTM3lYVVlSQl9rTjVLN0ZqOFdRUmxPa3hQazJoMC1mNkpyX0FrdXBSZzB5YmpOQSJd

)

有人知道这是否可以做到吗?如果我没有多大意义,我很抱歉,我对 c# 还很陌生

【问题讨论】:

我假设您的程序不工作。您能否更具体地说明哪个部分不起作用? 首先,我不确定这是否是拼写错误,但您在 public Form_1 方法上缺少一个右花括号... DataTable userstable = new DataTable(); 移动到类范围。并将Fill 之后的3 行移至Form load。 【参考方案1】:

据我所知,您已经实现了自己的目标。 File.ReadAllLines 返回一个字符串数组,每行一个字符串。所以此时在你的代码中......

        foreach (var line in File.ReadAllLines(filepath))
        
            if (line.Contains("[email]") || line.Contains("[password]"))
            
                // ** RIGHT HERE, BABY **

line 正是您想要的:一个包含文件中整行的字符串,供您“使用”。

【讨论】:

以上是关于卡住试图从文本文件输入字符串的主要内容,如果未能解决你的问题,请参考以下文章

试图从一个文本文件中的行创建一个地图[字符串,字符串],不断收到错误[复制]

从文本文件中获取价值

字符输入流读取文本文件ReaderFileReaderBufferedReader

字符输入流读取文本文件ReaderFileReaderBufferedReader

来自文本文件的矩阵输入(python 3)

Unicode / UTF-8文本文件:Windows控制台上的乱码(试图显示希伯来语)