C#通过关键字访问文本特定内容

来源:互联网 发布:天网 软件 编辑:程序博客网 时间:2024/05/16 05:33
//path:如"assets/a.txt"  ,  key:关键字string Read_File(string path, string key){StreamReader sr = null;try{sr = File.OpenText (path);}catch(Exception e){return "加载失败";}string text="";string line;string nextLine;while ((line=sr.ReadLine())!=null){if(line == key){while((nextLine=sr.ReadLine ())!=null && nextLine!="//"){text += nextLine+"\n";}}}sr.Close ();sr.Dispose ();return text;}


a.txt:

...

note

abcd

efg

//

...


string text = ReadFile("a.txt", "note");

则text = "abcd\nefg\n";


0 0