C# 在txt文件中搜索并将结果放入文本框

C# 在txt文件中搜索并将结果放入文本框,c#,file,C#,File,我有一个这样的文本文件: ; "one" id_number:*=344E6F4D6F7265486178785454332100 ; "two" id_number:*=3536336A775E3825246E773543563437 ; "three" id:_number*=BDBD2EB72D82473DBE09F1B552A8983 同样的方法,我想在文件中搜索标题(两个),然后给我它的id号,我想把id号放在文本框中 我曾经 string[] s = File.ReadAl

我有一个这样的文本文件:

; "one"
id_number:*=344E6F4D6F7265486178785454332100

; "two"

id_number:*=3536336A775E3825246E773543563437

; "three"

id:_number*=BDBD2EB72D82473DBE09F1B552A8983
同样的方法,我想在文件中搜索标题(两个),然后给我它的id号,我想把id号放在
文本框中

我曾经

string[] s = File.ReadAllLines("MyFilePath.txt");
List<byte[]> byteArrays = new List<byte[]>();
foreach (string st in s.Where(x => x.Trim().StartsWith("id_number:*=")).Select(x => x.Skip(12)))
{
    byte[] b = new byte[(int)((st.Length + 1) / 2)];
    for (int i = 0; i < (int)((st.Length + 1) / 2); i++)
    {
        var byteString = (st.Skip(2 * i).length > 2) ? st.Skip(2 * i).Take(2) : "0" + st.Skip(2 * i);
        var bt = Convert.ToByte(byteString, 16);
        b[i] = bt;
    }
    byteArrays.Add(b);
string[]s=File.ReadAllLines(“MyFilePath.txt”);
List byteArrays=新列表();
foreach(s.Where中的字符串st(x=>x.Trim().StartsWith(“id_number:=”))。选择(x=>x.Skip(12)))
{
字节[]b=新字节[(int)((st.Length+1)/2)];
对于(int i=0;i<(int)((st.Length+1)/2);i++)
{
var byteString=(st.Skip(2*i).长度>2)?st.Skip(2*i).取(2):“0”+st.Skip(2*i);
var bt=Convert.ToByte(byteString,16);
b[i]=bt;
}
增加(b);
但它不起作用

常量字符串f=@“C:\test.txt”;
    const string f = @"C:\test.txt";

    static void Main(string[] args)
    {

        // 1
        // Declare new List.
        List<string> lines = new List<string>();

        // 2
        // Use using StreamReader for disposing.
        using (StreamReader r = new StreamReader(f))
        {
            // 3
            // Use while != null pattern for loop
            string line;
            while ((line = r.ReadLine()) != null)
            {

                // 4
                // Insert logic here.
                // ...
                // "line" is a line in the file. Add it to our List.

                if (line.Trim() == "")
                    continue;

                lines.Add(line);
            }
        }

        // 5

        string stringValue = "two";
        int correctIndex = -1;
        // Print out all the lines.
        for (int i=0; i<lines.Count; i++)
        {
            if (lines[i].Contains(stringValue))
            {
                correctIndex = i;
            }
        }

        if(correctIndex == -1)
        {
            Console.WriteLine("item is not found");
        }
        else
        {
            //here you will need probably little more cleaning of the string. If the strings are on 1 line you should search for lines[correctIndex]
            Console.WriteLine(lines[correctIndex + 1]);
        }
    }
}
静态void Main(字符串[]参数) { // 1 //宣布新名单。 列表行=新列表(); // 2 //使用StreamReader进行处理。 使用(StreamReader r=新StreamReader(f)) { // 3 //对循环使用while!=null模式 弦线; 而((line=r.ReadLine())!=null) { // 4 //在这里插入逻辑。 // ... //“行”是文件中的一行。请将其添加到我们的列表中。 如果(line.Trim()==“”) 继续; 行。添加(行); } } // 5 字符串stringValue=“两”; int校正指数=-1; //把所有的行都打印出来。
对于(int i=0;i@Rhumborl我编辑并放置了代码,但没有工作。我已经编辑了你的标题。请参见“”,其中的共识是“不,他们不应该”@user3227682如果你不使用控制台没关系,你可以把代码放在
Main
anywhere@user3227682可能您的文件名不是test,但您可以更改它。复制代码并在任何地方使用它。Rhumborl已经指出了这一点。使用时出现错误(StreamReader r=new StreamReader(f))在f@mybirthname@user3227682可能是因为您为文件写入了错误的路径。此外,这也可以正常工作,下次读取异常并尝试修复它。谢谢您,一切正常,但我需要第三行@mybirthname
string s, result;
bool bl = false;

using (StreamReader sr = new StreamReader("....txt"))
{
    while ((s = sr.ReadLine()) != null)
    {
        if(s.Contains ("two"))
        {
            bl = true;
        }

        if (s.Contains("id_number") && bl == true)
        {
            result = s.Replace("id_number:*=", "");

            textBox1.Text = result;

            break;
        }
    }
}