如何使用c#读取特定文件的详细信息

时间:2013-11-05 11:13:14

标签: text-files c#-2.0

如何使用c#...

阅读特定部分的详细信息

我是c#的新手,我必须根据方括号“[]”之间标记的部分从文本文件中读取详细信息..文件看起来像

[Header]
This is the header info for the file
[Body]
This is the body information for the provided file
and it contains many information for the file
[Summary]
Summary for the file.

我需要阅读这些部分的每一个细节(例如[Header],[Body])..

对此方向的任何帮助都非常感谢...

1 个答案:

答案 0 :(得分:0)

假设这些标题之间的文本不包含括号,您可以这样做:

Dictionary<String,String> content = new Dictionary<String,String>();

String text = @"[Header]
This is the header info for the file
[Body]This is the body information for the provided file and it contains many information for the file
[Summary]Summary for the file.";

foreach (String section in text.Split("[".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
{
    String[] sectionParts = section.Split(']');
    content.Add(sectionParts[0], sectionParts[1]);
}

Dictionary将包含文件内容作为标题文本对