只创建了一个文件 为什么?

发布于 2025-01-05 16:45:52 字数 1189 浏览 3 评论 0原文

在这里,我从数据库读取数据,循环结果,并将包含 HTML 字符串行的列值传递给函数。此函数从 HTML 输入中提取 标记和内容。

当添加断点进行调试并且我单步执行代码时,一切正常。当断点被移除时,只创建一个文件,但不会抛出异常。代码:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        try
        {
            getBody(ds.Tables[0].Rows[i]["MailText"].ToString());
        }
        catch(Exception exp)
        {
            Response.Write(exp.ToString());
        }
    }


public void getBody(string html)
{
    try
    {
        HtmlDocument HD = new HtmlDocument();
        HD.LoadHtml(html);
        string output = HD.DocumentNode.SelectSingleNode("//body") == null ? HD.DocumentNode.InnerHtml : HD.DocumentNode.SelectSingleNode("//body").InnerHtml;
        using (StreamWriter sw = new StreamWriter(Server.MapPath("OnlyBody/") + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".txt"))
        {
            sw.Flush();
            sw.Write(output);
            if (sw != null)
            {
                sw.Close();
                sw.Dispose();
            }
        }

    }
    catch (Exception exp)
    {
        throw exp;
    }
    finally
    {

    }
}

起初我以为问题出在文件流上,但即使在处理 StreamWriter 之后,错误仍然存​​在。

Here I read data from database, loop over results, and pass a column value containing an HTML string row to a function. This function extracts <body> tag and contents from HTML input.

When a breakpoint is added to debug and I step through code, everything works properly. When breakpoint is removed, only one file is created but no exceptions are thrown. Code:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        try
        {
            getBody(ds.Tables[0].Rows[i]["MailText"].ToString());
        }
        catch(Exception exp)
        {
            Response.Write(exp.ToString());
        }
    }


public void getBody(string html)
{
    try
    {
        HtmlDocument HD = new HtmlDocument();
        HD.LoadHtml(html);
        string output = HD.DocumentNode.SelectSingleNode("//body") == null ? HD.DocumentNode.InnerHtml : HD.DocumentNode.SelectSingleNode("//body").InnerHtml;
        using (StreamWriter sw = new StreamWriter(Server.MapPath("OnlyBody/") + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".txt"))
        {
            sw.Flush();
            sw.Write(output);
            if (sw != null)
            {
                sw.Close();
                sw.Dispose();
            }
        }

    }
    catch (Exception exp)
    {
        throw exp;
    }
    finally
    {

    }
}

At first I thought the problem was with file stream, but even after disposing StreamWriter the error persists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2025-01-12 16:45:52

当您使用断点进行调试时,此代码块将返回不同的结果:

DateTime.Now.ToString("ddMMyyyyhhmmss")

如果没有断点,秒 (ss) 不会增加,因为代码执行得太快。

When you debug with breakpoints, this block of code will return different results:

DateTime.Now.ToString("ddMMyyyyhhmmss")

Without breakpoints, the seconds (ss) don't increment because the code is executing too quickly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文