需要解析字符串直到文件末尾

时间:2010-03-11 16:45:24

标签: c# asp.net string

我有以下代码..我需要按照下面显示的注释代码循环遍历文件的结尾我是如何做到的?

namespace BVParser
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            StreamReader sr = new StreamReader(@"D:\Jaison\JaisonWorking\EmailParse\Sample.BV.application.Mails\Sample.BV.application.Mails\ToTest\Test\SourceAscii.msg", Encoding.Unicode);
            string message = sr.ReadToEnd();
            StreamWriter sw = new StreamWriter(@"D:\Jaison\JaisonWorking\EmailParse\Sample.BV.application.Mails\Sample.BV.application.Mails\ToTest\Test\DestAsciiOutNewWOEncodingUnicode.txt");
            sw.Write(message);
            sw.Close();
            sr.Close();
            // StreamReader srseek = new StreamReader(@"D:\Jaison\JaisonWorking\EmailParse\Sample.BV.application.Mails\Sample.BV.application.Mails\ToTest\Test\DestAsciiOutNewWOEncodingUnicode6.txt");

            FileStream fs = new FileStream(@"D:\Jaison\JaisonWorking\EmailParse\Sample.BV.application.Mails\Sample.BV.application.Mails\ToTest\Test\DestAsciiOutNewWOEncodingUnicode6.txt", FileMode.Open, FileAccess.Read);
            StreamReader r = new StreamReader(fs);
            int index = message.IndexOf("prod-");
            //Label2.Text = index.ToString();
            Cluster.Text = message.Substring(index + 5, 2);
            int indexend = message.IndexOf(" ", index);
            int indexdiff = indexend - index;
            Servers.Text = message.Substring(index, indexdiff);

           // Loops should start here.. checking indexat("EOF")  

          While ()
          {
             int exindex = message.IndexOf("Exception:");
                int checkspace = exindex;
                checkspace--;
                if (checkspace == ' ')
                {
                    exindex = message.IndexOf("Exception:", exindex);
                }

                int trav = exindex;
                while (message[trav] != '.') // || message[trav] != ' '
                {
                    trav--;
                }

                int expdiff = exindex - trav + 9;
                Exceptions.Text = message.Substring(trav, expdiff);
                int lastdescindex = message.IndexOf('\n', exindex);
                int firstdescindex = exindex + 10;
                int diffdesc = lastdescindex - firstdescindex;
                Desc.Text = message.Substring(firstdescindex, diffdesc);

            // } Loop should end here.


            fs.Close();

        }
    }
}

2 个答案:

答案 0 :(得分:1)

这是我做过的一个例子......

    StreamReader reader = File.OpenText(filename);
    string line = null
    while ((line = reader.ReadLine()) != null)
    {
    // ... your stuff here
    }
    reader.Close();
    reader.Dispose();

答案 1 :(得分:0)

我还建议使用StreamReader。 由于它是IDisposable,因此您也可以使用'using'语句来构建它:

using (var reader = new StreamReader(path))
{
    string line = null
    while ((line = reader.ReadLine()) != null)
    {
        // do something
    }
}