如何使用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])..
对此方向的任何帮助都非常感谢...
答案 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
将包含文件内容作为标题文本对