您的位置:首页 > 编程语言 > C#

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

2014-06-13 22:07 399 查看
//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";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: