我正在创建一个程序,允许您解析用户名和密码的文本文件,然后针对数据库运行它们以返回其名字,姓氏和部门名称。我已经让程序逐行解析一个文本文件,我已经创建了if if语句来检查该行是否包含" [email]"或" [密码]"。我想要做的是如果该行包含其中一行,将整行复制到我可以使用的字符串中。 这是我到目前为止的代码,我承认这不是最好的,因为我还在学习。
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#
相当陌生答案 0 :(得分:0)
我可以告诉你,我已经完成了你的目标。 File.ReadAllLines返回一个字符串数组,每行一个字符串。所以在你的代码的这一点......
foreach (var line in File.ReadAllLines(filepath))
{
if (line.Contains("[email]") || line.Contains("[password]"))
{
// ** RIGHT HERE, BABY **
line
正是您想要的:一个字符串,其中包含文件中的整行以及#34;使用"。