从文本文件中获取价值

问题描述 投票:1回答:2

我试图从txt文件中获取一些文本(如果存在)。

文本文件如下所示:

"account name","Server name"
"user1","serverX"
"user2","serverY"
"user3","serverZ"

现在我想要的是当点击一个按钮时,它从输入框中获取一个值并在文本文件中找到它,如果它存在,它会向我显示旁边的“服务器数据”。否则它显示“notfound”

例如:我输入“user2”作为输入,单击按钮并返回:“serverY”

这是我在的地方:

using (StreamReader sr = new StreamReader("C:\\test\\TestFile.txt"))
{
    String line = await sr.ReadToEndAsync();
    if (lines.Contains("inputbox") {
        // How I'm telling him to get the server value
    } else {
        console.writeline("notfound");
    }

现在,if语句在我尝试时起作用,它在文本中找到输入框,但不知道如何告诉他获取旁边的数据。

c# stream
2个回答
0
投票

请尝试以下代码:

    public static string GetServerNameForUser(string value)
    {
        // this part will wrap input with " e.g. "value"
        value = "\"" + value + "\"";
        string[] data = File.ReadAllLines("C:\\test\\TestFile.txt");
        foreach(string log in data)
        {
            string[] temp = log.Split(',');
            if(temp[0].Equals(value))
            {
                return temp[1];
            }
        }
        return "Not Found";
    }

方法2。

    public static string GetServerNameForUser(string value)
    {
        string[] data = File.ReadAllLines("C:\\test\\TestFile.txt");
        foreach(string log in data)
        {
            string[] temp = log.Split(',');
            if(temp[0].Contains(value))
            {
                return temp[1];
            }
        }
        return "Not Found";
    }

方法3.异步方式

    public static async Task<string> GetServerNameForUser(string value)
    {
        string[] data = await ReadAllLinesAsync("C:\\test\\TestFile.txt");
        foreach(string log in data)
        {
            string[] temp = log.Split(',');
            if(temp[0].Contains(value))
            {
                return temp[1];
            }
        }
        return "Not Found";
    }

    public static Task<string[]> ReadAllLinesAsync(string path)
    {
        return ReadAllLinesAsync(path, Encoding.UTF8);
    }

    public static async Task<string[]> ReadAllLinesAsync(string path, Encoding encoding)
    {
        var lines = new List<string>();

        // Open the FileStream with the same FileMode, FileAccess
        // and FileShare as a call to File.OpenText would've done.
        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
        using (var reader = new StreamReader(stream, encoding))
        {
            string line;
            while ((line = await reader.ReadLineAsync()) != null)
            {
                lines.Add(line);
            }
        }

        return lines.ToArray();
    }

0
投票

尝试使用linq,在这种情况下你可以写这样的东西

    public static string SearchData(IEnumerable<string> lst, string searchString)
    {
        var result = lst.FirstOrDefault(x => x.Split(',')[0].Contains(searchString));
        return result == null ? "not found" : result.Split(',')[1];
    }

然后调用方法

   var searchString = Console.ReadLine();
   var result = SearchData(System.IO.File.ReadAllLines("data.txt"), searchString);
   Console.WriteLine(result);
   Console.ReadKey();

这是结果:

enter image description here

如果你想按整个词搜索,而不是按部分搜索,只需将Contains替换为Equals

热门问题
推荐问题
最新问题