C# 使用C从文本文件中获取值#

C# 使用C从文本文件中获取值#,c#,C#,我有一个包含以下数据的文本文件: name = Very well sir age = 23 profile = none birthday= germany manufacturer = Me 我想得到个人资料,生日和制造商的价值,但似乎不能得到它的权利。我成功地将文件包含到我的程序中,但它停止了。我只是不知道该如何清理文本文件 以下是我当前的代码:我建议不要使用ReadToEnd,而是读取每一行并在每一行文本上执行string.Split('='),然后执行string.Trim()。每行

我有一个包含以下数据的文本文件:

name = Very well sir
age = 23
profile = none
birthday= germany
manufacturer = Me
我想得到个人资料,生日和制造商的价值,但似乎不能得到它的权利。我成功地将文件包含到我的程序中,但它停止了。我只是不知道该如何清理文本文件


以下是我当前的代码:

我建议不要使用ReadToEnd,而是读取每一行并在每一行文本上执行string.Split('='),然后执行string.Trim()。每行应该有两个值,第一个是键,第二个是值

例如,在您的阅读循环中:

List<string[]> myList = new List<string[]>();
string[] splits = nextLine.Split('=');
if (splits.Length == 2)
    myList.Add(splits);
List myList=new List();
字符串[]splits=nextLine.Split('=');
如果(拆分长度==2)
添加(拆分);

我建议不要使用ReadToEnd,而是读取每一行并在每一行文本上执行string.Split('='),然后执行string.Trim()。每行应该有两个值,第一个是键,第二个是值

例如,在您的阅读循环中:

List<string[]> myList = new List<string[]>();
string[] splits = nextLine.Split('=');
if (splits.Length == 2)
    myList.Add(splits);
List myList=new List();
字符串[]splits=nextLine.Split('=');
如果(拆分长度==2)
添加(拆分);

您需要先拆分为行,然后再拆分行:

StreamReader reader = new StreamReader(filePath);
string line;
while(null != (line=reader.Read())
{
    string[] splitLine = strLines.Split('='); 
    //Code to find specific items based on splitLine[0] - Example
    //TODO: Need a check for splitLine length
    case(splitLine[0].ToLower().Trim())
    {
        case "age": { age = int.Parse(splitLine[1]);
    }
}

reader.Dispose();

这将为您提供一个良好的开端。

您需要先拆分为行,然后再拆分行:

StreamReader reader = new StreamReader(filePath);
string line;
while(null != (line=reader.Read())
{
    string[] splitLine = strLines.Split('='); 
    //Code to find specific items based on splitLine[0] - Example
    //TODO: Need a check for splitLine length
    case(splitLine[0].ToLower().Trim())
    {
        case "age": { age = int.Parse(splitLine[1]);
    }
}

reader.Dispose();
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var data = File
            .ReadAllLines("test.txt")
            .Select(x => x.Split('='))
            .Where(x => x.Length > 1)
            .ToDictionary(x => x[0].Trim(), x => x[1]);

        Console.WriteLine("profile: {0}", data["profile"]);
        Console.WriteLine("birthday: {0}", data["birthday"]);
        Console.WriteLine("manufacturer: {0}", data["manufacturer"]);
    }
}

这应该是一个好的开始。

什么叫“清理文本文件”?清除文件中的所有内容?除去我想要的部分以外的所有内容。你说的“清除文本文件”是什么意思?清除文件的所有内容?删除所有内容,但不删除我想要的部分。我知道这很优雅,但我仍在与LINQ syntaxI进行斗争,以获取“给定的密钥不在字典中”。根据此解决方案。@Victor,您的文件中可能有空格。我已经更新了我的答案,在键上的
ToDictionary
扩展方法中添加了
.Trim()
,该扩展方法应删除这些空格。还要确保您的文件包含一行具有给定密钥的代码。现在可以正常工作了!非常感谢@Darin-是的,我确实需要学习-目前正在学习objective-c,我的大脑中只有这么多空间,我的c#知识被卡住了。NET2I知道这很优雅,但我仍在与LINQ syntaxI斗争,以获得“给定的密钥不在字典中”。从这个解决方案中。@Victor,您的文件中可能有空格。我已经更新了我的答案,在键上的
ToDictionary
扩展方法中添加了
.Trim()
,该扩展方法应删除这些空格。还要确保您的文件包含一行具有给定密钥的代码。现在可以正常工作了!非常感谢@达林-是的,我确实需要学习-目前正在学习objective-c,而我的大脑中只有这么多空间,我的c#知识被卡在了里面。NET2
using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        var data = File
            .ReadAllLines("test.txt")
            .Select(x => x.Split('='))
            .Where(x => x.Length > 1)
            .ToDictionary(x => x[0].Trim(), x => x[1]);

        Console.WriteLine("profile: {0}", data["profile"]);
        Console.WriteLine("birthday: {0}", data["birthday"]);
        Console.WriteLine("manufacturer: {0}", data["manufacturer"]);
    }
}