본문 바로가기

Web_Application/C#

[c#] StreamWriter 로 텍스트 파일 생성/쓰기



StreamWriter 로 텍스트 파일 생성/쓰기


public static void SaveFile(string filePath, string saveData = null) 

{

    if (!File.Exists(filePath))

    {

        using (StreamWriter sw = File.CreateText(filePath))

        {

            sw.Write(saveData);

            sw.AutoFlush = true;

        }

    }

    else

    {

        using (StreamWriter sw = File.AppendText(filePath))

        {

            sw.Write("\r\n" + saveData);

            sw.AutoFlush = true;

        }

    }       

}



참조 : StreamWriter MSDN