日期:2014-05-20  浏览次数:20815 次

如何读出文件?
BSS   release                                                 :   8
Name   of   BSC                                                 :   SQ_BSCG2_1
Type   of   Measurement                                 :   RT180_Traffic   Flow   Measurements
Measurement   begin   date   and   time         :   2007-02-09   20:00
Measurement   end   date   and   time             :   2007-02-10   00:00


文件格式如上所式,请问如何将冒号后面的字符串读到5个变量中?

请给出完整代码,谢谢


------解决方案--------------------
StreamReader.ReadLine行读
IndexOf查找
Substring截取
------解决方案--------------------
楼上正确
------解决方案--------------------
string s1,s2,s3,s4,s5;
System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath( "aa.txt "));
string str= sr.ReadToEnd();
sr.Close();
string[] arr = str.Split(new char[] { '\r ', '\n '});
s1 = arr[0].Split( ': ')[1];
s2 = arr[1].Split( ': ')[1];
s3 = arr[2].Split( ': ')[1];
s4 = arr[3].Split( ': ')[1];
s5 = arr[4].Split( ': ')[1];
------解决方案--------------------
StreamReader sr = new StreamReader( "TextFile1.txt ");
Dictionary <string, string> items = new Dictionary <string, string> ();
string line = string.Empty;
while ((line = sr.ReadLine()) != null)
{
Match match = Regex.Match(line, @ "(.+?):(.*) ");
if (match.Success)
{
items.Add(match.Groups[1].Value.Trim(), match.Groups[2].Value.Trim());
}
}
------解决方案--------------------
up