Reputation: 605
I am new to C#. How can I write data into one file? This is my code so far:
public void convertHTML(string strData, string strTitle)
{
int position = strTitle.LastIndexOf('.');
strTitle = strTitle.Remove(position);
strTitle= strTitle + ".html";
StreamWriter sw = new StreamWriter(strTitle); //strTitle is FilePath
sw.WriteLine("<html>");
sw.WriteLine("<head><title>{0}</title></head>",strTitle);
//MessageBox.Show("this editor");
sw.WriteLine("<body>");
sw.WriteLine(strData); //strData is having set of lines
sw.WriteLine("</body>");
sw.WriteLine("</html>");//*/
lstHtmlFile.Items.Add(strTitle);
}
it will simply create one blank html file it won't have any data
Upvotes: 0
Views: 400
Reputation: 29000
You can add block using in order to clean your non managed object
using (var streamWriter = new StreamWriter(strTitle))
{
....
}
Link : http://msdn.microsoft.com/fr-fr/library/vstudio/yh598w02.aspx
Upvotes: 1
Reputation: 56727
You need to flush and close the StreamWriter
:
using (StreamWriter sw = new StreamWriter(strTitle))
{
sw.WriteLine("<html>");
sw.WriteLine("<head><title>{0}</title></head>",strTitle);
sw.WriteLine("<body>");
sw.WriteLine(strData);
sw.WriteLine("</body>");
sw.WriteLine("</html>");
}
Using using
does the trick.
Upvotes: 3