0

私はC#を初めて使用します。1つのファイルにデータを書き込むにはどうすればよいですか?これはこれまでの私のコードです:

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);
    }

データを含まない空のhtmlファイルを1つ作成するだけです。

4

2 に答える 2

3

あなたはフラッシュして閉じる必要があります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するとトリックが行われます。

于 2012-09-20T08:16:46.870 に答える
1

あなたはあなたのをきれいにするためにを使用してブロックを追加することができますnon managed object

using (var streamWriter = new StreamWriter(strTitle))
{

 ....
}

リンク: http: //msdn.microsoft.com/fr-fr/library/vstudio/yh598w02.aspx

于 2012-09-20T08:38:51.213 に答える