B. Clay Shannon B. Clay Shannon - 4 months ago 9
C# Question

Why does my call to file.WriteLine() fail to write all contents of the StringBuilder to the file?

I'm trying to write an html file to disk via a StringBuilder and File.WriteLn(), but the file is written lacking a few lines, and is thus useless. My code is:

private void BuildAndWriteHTMLFile()
{
StringBuilder sb = new StringBuilder();
sb.Append("<!DOCTYPE html>");
sb.Append("<html lang=\"en\">");
sb.Append("<head>");
sb.Append("<meta charset=\"utf-8\">");
sb.Append("<title>Green Bay Packers 2013 Schedule (All times Central)</title>");
sb.Append("<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js\"></script> ");
sb.Append("<script src=\"http://maps.google.com/maps/api/js?sensor=false\"></script> ");
sb.Append("<script src=\"gomap.js\"></script> ");
sb.Append("<!--[if IE]>");
sb.Append("<script src=\"http://html5shiv.googlecode.com/svn/trunk/html5.js\"></script>");
sb.Append("<![endif]-->");

. . . // oodles on noodly code elided for lack of reason to leave it in

sb.Append("this.singleMarker = false;");
sb.Append("this.lockGeocode = false;");
sb.Append("this.markers = [];");
sb.Append("this.tmpMarkers = [];");
sb.Append("this.geoMarkers = [];");
sb.Append("},");

sb.Append("isVisible: function(latlng) {");
sb.Append("return this.map.getBounds().contains(latlng);");
sb.Append("}");
sb.Append("}");
sb.Append("})(jQuery);");

sb.Append("$(function() {");

sb.Append("$(\"#map\").goMap({ ");
sb.Append("latitude: 36.75, ");
sb.Append("longitude: -100, ");
sb.Append("maptype: 'ROADMAP',");
sb.Append("zoom: 5");
sb.Append("}); ");

sb.Append("$.goMap.createMarker({");
sb.Append("address: 'Green Bay, Wisconsin',");
sb.Append("title: 'Lambeau Field, Green Bay, WI',");
sb.Append("html: '<h1>Green Bay Packers Home Games</h1><p class=\"away\">Game 1, Week 1) Sunday Sept. 8 at SAN FRANCISCO 49ERS 3:25 p.m.</p><p>Game 2, Week 2) Sunday Sept. 15 WASHINGTON REDSKINS Noon</p><p class=\"away\">Game 3, Week 3) Sunday Sept. 22 at CINCINNATI BENGALS Noon</p><p>Game 4, Week 5) Sunday Oct. 6 DETROIT LIONS Noon</p><p class=\"away\">Game 5, Week 6) Sunday Oct. 13 at BALTIMORE RAVENS Noon</p><p>Game 6, Week 7) Sunday Oct. 20 CLEVELAND BROWNS 3:25 p.m.</p><p class=\"away\">Game 7, Week 8) Sunday Oct. 27 at MINNESOTA VIKINGS 7:30 p.m.</p><p>Game 8, Week 9) Monday Nov. 4 CHICAGO BEARS 7:40 p.m.</p><p>Game 9, Week 10) Sunday Nov. 10 PHILADELPHIA EAGLES Noon</p><p class=\"away\">Game 10, Week 11) Sunday Nov. 17 at NEW YORK GIANTS 7:30 p.m. (flex)</p><p>Game 11, Week 12) Sunday Nov. 24 MINNESOTA VIKINGS Noon (flex)</p><p class=\"away\">Game 12, Week 13) Thursday Nov. 28 at DETROIT LIONS 11:30 a.m. </p><p>Game 13, Week 14) Sunday Dec. 8 ATLANTA FALCONS 7:30 p.m. (flex)</p><p class=\"away\">Game 14, Week 15) Sunday Dec. 15 at DALLAS COWBOYS 3:25 p.m. (flex)</p><p>Game 15, Week 16) Sunday Dec. 22 PITTSBURGH STEELERS 3:25 p.m. (flex)</p><p class=\"away\">Game 16, Week 17) Sunday Dec. 29 at CHICAGO BEARS Noon (flex)</p><p class=\"away\">SUPER BOWL Sunday Feb. 2, 2014 vs ?</p>'");
//sb.Append("html: '<h1>Green Bay Packers Home Games</h1><p class=\"away\">Game 1, Week 1) Sunday Sept. 8 at SAN FRANCISCO 49ERS 3:25 p.m.</p><p>Game 2, Week 2) Sunday Sept. 15 WASHINGTON REDSKINS Noon</p><p class=\"away\">Game 3, Week 3) Sunday Sept. 22 at CINCINNATI BENGALS Noon</p><p>Game 4, Week 5) Sunday Oct. 6 DETROIT LIONS Noon</p><p class=\"away\">Game 5, Week 6) Sunday Oct. 13 at BALTIMORE RAVENS Noon</p><p>Game 6, Week 7) Sunday Oct. 20 CLEVELAND BROWNS 3:25 p.m.</p><p class=\"away\">Game 7, Week 8) Sunday Oct. 27 at MINNESOTA VIKINGS 7:30 p.m.</p><p>Game 8, Week 9)");
//sb.Append("Monday Nov. 4 CHICAGO BEARS 7:40 p.m.</p><p>Game 9, Week 10) Sunday Nov. 10 PHILADELPHIA EAGLES Noon</p><p class=\"away\">Game 10, Week 11) Sunday Nov. 17 at NEW YORK GIANTS 7:30 p.m. (flex)</p><p>Game 11, Week 12) Sunday Nov. 24 MINNESOTA VIKINGS Noon (flex)</p><p class=\"away\">Game 12, Week 13) Thursday Nov. 28 at DETROIT LIONS 11:30 a.m. </p><p>Game 13, Week 14) Sunday Dec. 8 ATLANTA FALCONS 7:30 p.m. (flex)</p><p class=\"away\">Game 14, Week 15) Sunday Dec. 15 at DALLAS COWBOYS 3:25 p.m. (flex)</p><p>Game 15, Week 16)");
//sb.Append("Sunday Dec. 22 PITTSBURGH STEELERS 3:25 p.m. (flex)</p><p class=\"away\">Game 16, Week 17) Sunday Dec. 29 at CHICAGO BEARS Noon (flex)</p><p class=\"away\">SUPER BOWL Sunday Feb. 2, 2014 vs ?</p>'");
sb.Append("});");

sb.Append("});");
sb.Append("</script>");
sb.Append("</body>");
sb.Append("</html>");

string nameOfFile = string.Format(@"C:\misc\mapcode_{0}", GetFileNameDateTimeExtension());
if (!File.Exists(nameOfFile))
{
StreamWriter file = new StreamWriter(@nameOfFile);
file.WriteLine(sb.ToString());
urlOfGeneratedFile = string.Format("file:///{0}", nameOfFile);
webBrowser1.Url = new Uri(urlOfGeneratedFile);
}
}


The problem is that the file that is written is a truncated version of what's in the StringBuilder. It first truncated the last few lines that had been appended (with the written file ending on "NEW YORK G"); when I commented out the last createMarker section just prior to the closing script, body, and html tags, the file ended a little above that on "this.lockGeocode = false;this.markers = [];this.tmpMarkers = [];this.geoM"

It's as if the write to file is being done before all the lines have been written to the StringBuilder.

Do I need to call Flush, or a Sleep method, or what?

Answer

You are not closing/disposing of the StreamWriter, the writer has a internal buffer that is not written out till you close the stream. Wrap the writer in a using statment to automaticly close the file when done.

if (!File.Exists(nameOfFile))
{
    using(StreamWriter file = new StreamWriter(@nameOfFile))
    {
        file.WriteLine(sb.ToString());
    }
    urlOfGeneratedFile = string.Format("file:///{0}", nameOfFile);
    webBrowser1.Url = new Uri(urlOfGeneratedFile);

}

However, your code could be simplied to just not use a StreamWriter and instead just call File.WriteAllText

if (!File.Exists(nameOfFile))
{
    File.WriteAllText(@nameOfFile,sb.ToString());
    urlOfGeneratedFile = string.Format("file:///{0}", nameOfFile);
    webBrowser1.Url = new Uri(urlOfGeneratedFile);

}

UPDATE: Actually, there will be a slight difference with File.WriteAllText, the file.WriteLine will add a extra newline at the end of the text, File.WriteAllText will not. If it is important to you that the newline is there add a sp.AppendLine(); before you call File.WriteAllText.