C# 如何检查在文本文件中找到字符串的位置

C# 如何检查在文本文件中找到字符串的位置,c#,C#,编辑:修正:)谢谢 我正在尝试创建一个登录代码,但在登录功能上遇到了一些问题。 我需要检查用户名位于文本文件中的哪一行。可能吗?如果没有,还有别的办法吗 编辑:添加代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[]

编辑:修正:)谢谢

我正在尝试创建一个登录代码,但在登录功能上遇到了一些问题。 我需要检查用户名位于文本文件中的哪一行。可能吗?如果没有,还有别的办法吗

编辑:添加代码

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 );

我假设您对这一行代码感兴趣:

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
string[]lines=System.IO.File.ReadAllLines(“C:\\Users\\Public\\Usernames.txt”)
int line_number=0;
对于(;行号<行.长度;行号++)
{
if(行[行编号]==寄存器)
打破
}
//您的行号现在包含在行号中
试试这个:

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++;
  }
}

你想要行号吗?如果你能包含一个用于查找字符串的代码示例,那会很有帮助,然后我们可以建议如何调整它以获得行号你说“我在其中找到字符串的行”--那么,你到底是如何找到字符串的?密码在哪里?而且,你也不太可能在意行号。也许您还关心与行号相关的其他事项?这是什么?对不起,如果我不清楚,我想看看我是否能找到文本文件中字符串的哪一行。如果这是不可能的,有没有另一种方法来查看用户名是否与密码匹配。您正在文本文件中存储普通用户名和密码?!您确实应该寻找另一种身份验证方法。