[C#] HTML 코드를 StringBuilder.Append 로 변경하기

HTML 코드

HTML 로 작성한 코드를 C# StringBuilder 로 변경하는 방법에 대해 알아보도록 하겠습니다.

StringBuilder.Append 로 변경

바탕화면 0.txt 에 있는 HTML 코드를 0!.txt 파일로 반환하는 예제는 다음과 같습니다~

private void Run()
{
    string line = string.Empty;
    StringBuilder s = new StringBuilder();
    
    string path = @"C:\Users\BEOMSANG\Desktop\0.txt";
    string createPath = @"C:\Users\BEOMSANG\Desktop\0!.txt";

    using (StreamReader reader = new StreamReader(path))
    {
        while ((line = reader.ReadLine()) != null)
        {
            //s.Append("s.AppendLine(@\"").Append(line.Replace("\"", "\"\"")).Append("\");").AppendLine();
            s.Append("s.AppendLine(\"").Append(line.Replace("\"", "\\\"")).Append("\");").AppendLine();
        }
    }

    string tmp = s.ToString();
    Console.WriteLine(tmp);

    using (StreamWriter file = File.CreateText(createPath))
    {
        file.Write(tmp);
    }               
}

댓글