从文本文件中获取价值

时间:2018-03-05 11:45:47

标签: c# stream

我试图从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语句在我尝试时起作用,它在文本中找到输入框,但不知道如何告诉他获取旁边的数据。

2 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分: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