如何检查用户输入是否在数据文件C#中并显示正确的消息?
这是名为“ChargeAccounts.txt”的数据文件。我需要在这个数据文件中搜索在文本框txtAccount中输入的用户帐号如何检查用户输入是否在数据文件C#中并显示正确的消息?,c#,asp.net,C#,Asp.net,这是名为“ChargeAccounts.txt”的数据文件。我需要在这个数据文件中搜索在文本框txtAccount中输入的用户帐号 我需要使用这个数组来完成这个任务 const int SIZE = 18; string[] acct = new string[SIZE]; 这是我的代码,它只会显示“帐户无效”。即使我从数据文件中输入了帐号 private void FindAccountNumber() { System.IO.Stream
我需要使用这个数组来完成这个任务
const int SIZE = 18;
string[] acct = new string[SIZE];
这是我的代码,它只会显示“帐户无效”。即使我从数据文件中输入了帐号
private void FindAccountNumber()
{
System.IO.StreamReader file =
new System.IO.StreamReader("ChargeAccounts.txt");
acct = File.ReadAllLines("ChargeAccounts.txt");
for (int i = 0; i < acct.Length; i++)
{
if (txtAccount.Text.Contains(acct[i]))
{
lblMessage.Text = "Account is valid";
}
else
{
lblMessage.Text = "Account is invalid.";
}
}
file.Close();
}
private void FindAccountNumber()
{
System.IO.StreamReader文件=
新System.IO.StreamReader(“ChargeAccounts.txt”);
acct=File.ReadAllLines(“ChargeAccounts.txt”);
对于(int i=0;i
您正在迭代所有元素,并为每次迭代更新lblMessage.Text
。因此,只有当txtcount.Text
包含文件中的最后一个数字时,才能获得有效状态。当找到有效数字时,您应该中断for
语句,它将如下所示
string message = "Account is invalid.";
for (int i = 0; i < acct.Length; i++)
{
if (txtAccount.Text.Contains(acct[i]))
{
message = "Account is valid";
break;
}
}
lblMessage.Text = message;
string message=“帐户无效。”;
对于(int i=0;i
string message = "Account is invalid.";
for (int i = 0; i < acct.Length; i++)
{
if (txtAccount.Text.Contains(acct[i]))
{
message = "Account is valid";
break;
}
}
lblMessage.Text = message;