读取程序c#的文本文件中的凭据
本文关键字:文件 文本 程序 读取 | 更新日期: 2023-09-27 18:21:59
这是我的程序,如果我输入用户名和密码,它就能正常工作:
try
{
var url = @"https://mail.google.com/mail/feed/atom";
var User = username;
var Pasw = password;
var encoded = TextToBase64(User + ":" + Pasw);
var myweb = HttpWebRequest.Create(url) as HttpWebRequest;
myweb.Method = "POST";
myweb.ContentLength = 0;
myweb.Headers.Add("Authorization", "Basic " + encoded);
var response = myweb.GetResponse();
var stream = response.GetResponseStream();
textBox1.Text += ("Connection established with" + User + Pasw);
}
catch (Exception ex)
{
textBox1.Text += ("Error connection. Original error: " + ex.Message);
现在我想要读取texfile的字符串,将它们拆分,并读取用户名和密码,格式如下:username:password。现在有我的代码:
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:''";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
string file_name = "";
file_name = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + file_name;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (StringReader reader = new StringReader(file_name))
{
// Loop over the lines in the string.
int count = 0;
string line;
while ((line = reader.ReadLine()) != null)
{
string[] data = line.Split(':');
string username = data[0].Trim();
string password = data[1].Trim();
count++;
/* Console.WriteLine("Line {0}: {1}", count, line); */
}
reader.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
打开用户选择的文件,然后尝试读取变量file_name,该变量不是文件名,而是well-kown文件夹的名称。也许你想要这个
try
{
if (openFileDialog1.FileName != string.Empty)
{
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
....
}
}
}
在相同的代码中,您使用StringReader,但您需要一个StreamReader来读取文件。StringReader获取在其构造函数中传递的值,并在ReadLine调用中返回。然后在冒号处拆分行,但这当然不是文件的内容。
您的代码中还有其他问题。例如,如何处理从该行加载的用户名和密码?它们被声明为局部变量,不在任何地方使用,因此在下一个循环中,它们被覆盖并丢失。
因此,UserData类可能是的答案
public class UserData
{
public string UserName {get; set;}
public string Password {get; set;}
}
并在表单全局级别声明
List<UserData> data = new List<UserData>
在你的循环
public void button1_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog1.FileName != string.Empty)
{
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
int count = 0;
string line;
while ((line = reader.ReadLine()) != null)
{
UserData d = new UserData();
string[] parts = line.Split(':');
d.UserName = parts[0].Trim();
d.Password = parts[1].Trim();
data.Add(d);
}
// At the loop end you could use the List<UserData> like a normal array
foreach(UserData ud in data)
{
Console.WriteLine("User=" + dd.UserName + " with password=" + dd.Password);
}
}
}
}
}
public void button2_Click(object sender, EventArgs e)
{
try
{
if(data.Count() == 0)
{
MessageBox.Show("Load user info first");
return;
}
var url = @"https://mail.google.com/mail/feed/atom";
var encoded = TextToBase64(data[0].UserName + ":" + data[0].Password);
.....
警告说明。当然,这只是演示代码。请记住,在真实场景中,以明文形式保存密码是一个很大的安全问题。这种影响与应用程序的上下文有关,但不应低估。更好的做法是存储密码值的哈希,并在需要比较密码
您正在从file_name
variable创建StringReader,它是(根据您的代码)
string file_name = "";
file_name = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + file_name;
并指向其他地方。
此外,您已经为使用open file dialog
选择的文件创建了流,但您还没有使用此流。