如何检查在文本文件中找到字符串的位置
本文关键字:字符串 位置 文件 文本 何检查 检查 | 更新日期: 2023-09-27 17:49:28
编辑:修复:)谢谢!
我想做一个登录代码,我在登录功能上遇到了一点麻烦。我需要检查哪一行用户名位于,在一个文本文件。这可能吗?如果没有,有没有别的办法?
编辑:添加代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int loginAttempt = 0;
using (System.IO.StreamWriter file = new System.IO.StreamWriter("C:''Users''Public''Usernames.txt", true))
{
file.WriteLine();
}
int userType = 0;
string retrievedUsername = String.Empty;
using (System.IO.StreamReader fileUsername = new System.IO.StreamReader("C:''Users''Public''Usernames.txt"))
{
retrievedUsername = fileUsername.ReadToEnd();
}
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Please note that this is a prototype, passwords are not hashed/encrypted ^_^. Report any bugs / flaws with this to andrew.yu6@hotmail.com, If I get any spam I will high five you in the face with a chair");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Welcome to the meData service! Ver. 0.01 Beta, made by mechron");
do
{
Console.WriteLine("Please enter login below or type register to register a new account on this device");
string loginUsername = Console.ReadLine();
if (loginUsername == "login")
{
Console.WriteLine("Type in your username");
string loginUser = Console.ReadLine();
}
else
{
if (loginUsername == "register")
{
int registerAttempt = 0;
Console.WriteLine("Choose your username!");
string registerUsername = Console.ReadLine();
bool containUser = System.IO.File.ReadAllLines("C:''Users''Public''Usernames.txt").Contains(registerUsername);
if (containUser == true) { Console.WriteLine("Error, username already exists! Please try again!"); } else { registerAttempt = 1; }
if( registerAttempt == 1 )
{
using (System.IO.StreamWriter files = new System.IO.StreamWriter("C:''Users''Public''Usernames.txt", true))
{
files.WriteLine(registerUsername);
}
}
if (registerAttempt == 1)
{
Console.WriteLine("Now choose your password!");
string loginPass = Console.ReadLine();
using (System.IO.StreamWriter files = new System.IO.StreamWriter("C:''Users''Public''Passwords.txt", true))
{
files.WriteLine(loginPass);
}
Console.WriteLine("Good! Now you will be returned to the start screen! Try to login there!");
}
}
else
{
Console.WriteLine("Error, command not recognized. Try again");
}
}
} while ( loginAttempt == 0 );
Try This:
if(loginUsername == "login")
{
Console.WriteLine("Type in your username");
string loginUser = Console.ReadLine();
int lineCount=1;
foreach(var line in File.ReadLines("LoginFile.txt"))
{
if(line.Contains(loginUser))
{
Console.WriteLine("UserName found in Line No = "+ lineCount);
break;
}
lineCOunt++;
}
}
我假设您对这行代码感兴趣:
bool containUser = System.IO.File.ReadAllLines("C:''Users''Public''Usernames.txt").Contains(registerUsername);
不,这不会告诉你行号。你必须自己用循环来做。
string[] lines = System.IO.File.ReadAllLines("C:''Users''Public''Usernames.txt")
int line_number= 0;
for(;line_number< lines.Length; line_number++)
{
if(lines[line_number] == registeredUser)
break;
}
//Your line number is now contained in line_number