简体   繁体   English

C#关闭StreamWriter

[英]C# close StreamWriter

Im trying to close a text file that I have created and then read this back into a HTMLDocumentClass. 我试图关闭我创建的文本文件,然后将其读回HTMLDocumentClass。

This is the code 这是代码

StreamWriter outfile = 
    new StreamWriter(StripHTMLComps.Properties.Settings.Default.TempFileName);
outfile.Write(HTML);
outfile.Close();

HTMLDocumentClass doc = null;
doc = new HTMLDocumentClass();
IPersistFile persistFile = (IPersistFile)doc;
persistFile.Load(StripHTMLComps.Properties.Settings.Default.TempFileName, 0);
GC.SuppressFinalize(persistFile);
int start = Environment.TickCount;
while (doc.readyState != "complete")
{
    if (Environment.TickCount - start > 10000)
    {
    }
}

The document says loading but never completes, I believe that it believes the document is still in being used by another process. 该文件说正在加载但从未完成,我相信它认为该文件仍在被另一个进程使用。

Any ideas? 有任何想法吗?

Wrap the creation of the string in a using statement to ensure proper disposal (part of which will release the file handle - thanks @Adam Houldsworth): 将创建的字符串包装在using语句中,以确保正确处理(其中一部分将释放文件句柄-感谢@Adam Houldsworth):

using(StreamWriter outfile = new StreamWriter(StripHTMLComps.Properties.Settings.Default.TempFileName))
{
   outfile.Write(HTML);
   // outfile.Close(); // not needed, as the disposal will close the file.
}

// read the file now

I believe the HTMLDocumentClass is from the WebBrowser. 我相信HTMLDocumentClass来自WebBrowser。

I'm not sure you can use without the browser control, but if you can, you do not give it CPU cycles to process the file. 我不确定您是否可以在没有浏览器控件的情况下使用,但如果可以,则不要给它提供CPU周期来处理文件。

Try adding an Application.DoEvents or Sleep somewhere. 尝试在某个地方添加Application.DoEventsSleep

If that does not work, add a WebBrowser to your form, make it load the html, and use the object the browser exposes. 如果这不起作用,则将WebBrowser添加到表单中,使其加载html,然后使用浏览器公开的对象。

If you are just interested in parsing the html tree, look at the Html Agility Pack . 如果您只想解析html树,请查看Html Agility Pack

Too many assumptions in there for me 对我来说太多的假设

using(FileStream fs = new FileStream(SomeName,...))
{
  StreamWriter s = new Streamwriter(fs);
  s.Write(HTML);
  s.Flush();
}

Now it should be okay to use. 现在应该可以使用了。 I have a policy of always opening / creating files with FileStream using explicit permission requests. 我有一个始终使用显式权限请求通过FileStream打开/创建文件的策略。 If somethings not right you want that bit to fall over, not some code twenty minutes later and ten yards deeper in. The other thing to think about is why you need HTMLDocumentClass, it's a helper wrapper for a com object for use in a web browser control, and chock full of weirdness. 如果不正确,则希望该位掉下来,而不是二十分钟后再加深十码的代码。要考虑的另一件事是为什么需要HTMLDocumentClass,它是com对象的辅助包装,可在Web浏览器中使用控制,并充满奇怪。

Can't confirm this, but I saw a report that in order to get the doc state to completed, you had to call DoEvents inside your while not completed loop. 无法确认这一点,但是我看到一个报告,为了使文档状态完成,您必须在未完成循环中调用DoEvents。

Somebody else witha similar issue 遇到类似问题的其他人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM