프로그램(C# etc.)

한글txt 파일 읽을 때 안깨지게 읽는 코드, 설명 필요 없어

질문폭격 2022. 12. 29. 10:40

네, 여기는 한글 txt 파일을 읽을 때 안 깨지게 읽는 코드를 제공해드리겠습니다. 아래 코드는 한글 txt 파일을 읽을 때 인코딩을 지정하는 예제입니다:

string filePath = @"C:\MyFolder\MyFile.txt";

try
{
    // 파일을 읽기 전용으로 엽니다.
    using (StreamReader reader = new StreamReader(filePath, Encoding.Default))
    {
        // 파일의 내용을 읽습니다.
        string text = reader.ReadToEnd();
        Console.WriteLine(text);
    }
}
catch (Exception ex)
{
    Console.WriteLine("예외가 발생했습니다: {0}", ex.Message);
}

위 코드는 한글 txt 파일을 읽기 전용으로 여는 StreamReader 객체를 생성할 때 인코딩을 지정합니

 

 

코멘트: 이게 되네!!!