Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文本文件中的不同位置读取数据_C# - Fatal编程技术网

C# 从文本文件中的不同位置读取数据

C# 从文本文件中的不同位置读取数据,c#,C#,我试图从两个不同的文本文件中获取一些数据成员名称 示例文本文件1 示例文本文件2 在这两种情况下,我都希望使用相同的C代码来读取名称DAVID。我遇到的问题是,在第二个文件中,名称位于另一行 这是我当前的代码,我需要两个文件都有一个解决方案 System.IO.StreamReader file =new System.IO.StreamReader(@"Path"); string line; List<string> lin

我试图从两个不同的文本文件中获取一些数据成员名称

示例文本文件1 示例文本文件2 在这两种情况下,我都希望使用相同的C代码来读取名称DAVID。我遇到的问题是,在第二个文件中,名称位于另一行

这是我当前的代码,我需要两个文件都有一个解决方案

        System.IO.StreamReader file =new System.IO.StreamReader(@"Path");
             string line;
       List<string> lines = new List<string>();
        while ((line = file.ReadLine()) != null)
        {
                if(line.ToUpper().Contains("MEMBER"))
            {                  

                    lines.Add(line);


            }
        }
            foreach (var l in lines)
        {
            Console.WriteLine(l); //Member: David
        }
我会用正则表达式

var name = Regex.Match(File.ReadAllText(filename), @"Member:[ \t\r\n]+(.+?)\r?\n")
           .Groups[1].Value;

那么问题出在哪里呢?你最好把这个贴在代码上。事实上,我想从不同的文本中得到成员的名字files@Bhargav那么,您当前的方法有什么问题?请描述您的解决方案不起作用的原因,我们可以尝试帮助您。@john按照我的方法,我只能访问文本文件1中的名称
        System.IO.StreamReader file =new System.IO.StreamReader(@"Path");
             string line;
       List<string> lines = new List<string>();
        while ((line = file.ReadLine()) != null)
        {
                if(line.ToUpper().Contains("MEMBER"))
            {                  

                    lines.Add(line);


            }
        }
            foreach (var l in lines)
        {
            Console.WriteLine(l); //Member: David
        }
var name = Regex.Match(File.ReadAllText(filename), @"Member:[ \t\r\n]+(.+?)\r?\n")
           .Groups[1].Value;