4

该程序所做的是转到网页,逐行读取源代码,剥离 html 标签/代码,然后将实际文本/信息写入文本文件。因为我希望文本文件只包含我想要的数据/信息而没有其他内容,所以我让它寻找一个特定的字符串,告诉它它找到了正确的部分,因此它可以开始写入文本文件,然后它寻找另一个字符串知道何时找到该部分的末尾。它在文件中的正确行正确启动和停止(我放入了一个计数器以确保它在正确的位置正确停止/启动),但是,它没有完成将所有信息写入文本文件. 它甚至不会在特定行的末尾停止——它通常会在一行的中间停止写入。例如,如果我希望它读取 4 行,

第 1 行的文本.. 第 2 行的文本.. li 的文本

并且停止,即使它应该读/写 4 行。

下面是我的代码:

private void button1_Click(object sender, EventArgs e)
{

    string tempString = "";
    string startString = "start string";
    string endString = "end string";
    bool startFlag = false;
    bool endFlag = true;
    string filename = @"C:\file.txt";

    System.IO.StreamWriter file = new System.IO.StreamWriter(filename);

    WebRequest request = WebRequest.Create("http://www.website.com/webpage.html");
    request.Credentials = CredentialCache.DefaultCredentials;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Console.WriteLine(response.StatusDescription);
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);

    while (endFlag)
    {
        tempString = reader.ReadLine();

        if (tempString.Contains(startString))
        {
            startFlag = true;
        }

        if (tempString.Contains("text"))
        {
            ...
        }

        if (tempString.Contains("other text"))
        {
            if (startFlag)
                file.WriteLine(tempString.Trim());
        }

        if (tempString.Contains("different text"))
        {
                if (startFlag && tempString.Length > 0)
                    file.WriteLine(tempString.Trim());
        }

        if (tempString.Contains(endString))
        {
            endFlag = false;
        }
    }

    MessageBox.Show("Done!", "Finished Writing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

    reader.Close();
    dataStream.Close();
    response.Close();

    Process.Start(filename);

    this.Close();
}

我已经尝试了几乎所有我能想到的东西,我尝试过使用刷新,我尝试过让它继续读/写超过表示该部分结束的字符串,但似乎没有任何效果。我知道我无论如何都不是最伟大的程序员,但我真的很难过。任何人都可以分享一些见解吗?非常感谢任何帮助!

4

3 回答 3

3

You have to Flush() your file ... Or use

using( System.IO.StreamWriter file = new System.IO.StreamWriter(filename)){
   ....
}
Process.Start(filename);
于 2012-07-30T23:38:16.227 回答
2

Your StreamWriter file is not closed or flushed before you call Start on the file name.

Either Close or Dispose file object. Or even better wrap it in using.

于 2012-07-30T23:38:28.860 回答
0
private void button1_Click(object sender, EventArgs e)
{

    string tempString = "";
    string startString = "start string";
    string endString = "end string";
    bool startFlag = false;
    bool endFlag = true;
    string filename = @"C:\file.txt";

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename))
    {
        WebRequest request = WebRequest.Create("http://www.website.com/webpage.html");
        request.Credentials = CredentialCache.DefaultCredentials;
        using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
        {
            Console.WriteLine(response.StatusDescription);
            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(dataStream))
                {
                    while (endFlag)
                    {
                        tempString = reader.ReadLine();

                        if (tempString.Contains(startString))
                        {
                            startFlag = true;
                        }

                        if (tempString.Contains("text"))
                        {
                            // ...
                        }

                        if (tempString.Contains("other text"))
                        {
                            if (startFlag)
                                file.WriteLine(tempString.Trim());
                        }

                        if (tempString.Contains("different text"))
                        {
                            if (startFlag && tempString.Length > 0)
                                file.WriteLine(tempString.Trim());
                        }

                        if (tempString.Contains(endString))
                        {
                            endFlag = false;
                        }
                    }

                    MessageBox.Show("Done!", "Finished Writing", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    reader.Close();
                }
                dataStream.Close();
            }
            response.Close();
        }
    }

    Process.Start(filename);

    this.Close();
}
于 2012-07-31T00:27:22.693 回答