C#에서 임베디드 리소스 텍스트 파일 읽기
이 자습서는 C# 프로그래밍 언어를 사용하여 포함된 리소스 텍스트 파일을 읽는 방법을 보여줍니다.
포함된 리소스 텍스트 파일 읽기
포함된 파일을 포함된 리소스라고 하며 System.Reflection 네임스페이스에 있는 Assembly 클래스를 사용하여 런타임에 이러한 파일에 액세스할 수 있습니다. 포함된 파일은 현재 프로젝트에 포함된 모든 파일에서 만들 수 있습니다.
임베디드 리소스 텍스트 파일을 읽으려면 다음 단계를 따라야 합니다.
-
폴더 및 파일 추가
먼저
files라는 폴더를 프로젝트에 추가합니다. 그런 다음 아래와 같이솔루션 탐색기의컨텍스트 메뉴에서 찾을 수 있는추가 -> 기존 항목옵션을 활용하여 해당 폴더에 포함된 파일을 추가합니다.폴더에 포함된 파일을 추가할 수 있습니다.

포함할 파일을 추가한 후
파일을 마우스 오른쪽 버튼으로 클릭한 다음속성을 클릭해야 합니다.
이제 아래 스크린샷에 따라
Build Action속성 값을Content에서 임베디드 리소스로 변경합니다.
-
윈도우 폼 만들기
먼저 Windows 양식에
TextBox를 추가하고속성에서txtTextBox로 이름을 지정합니다.
이제
btnText라는 버튼을 추가하고 아래와 같이 버튼에Read Embed Text텍스트를 표시합니다.
-
Windows Form 코드 작성
먼저 다음 라이브러리를 가져와야 합니다.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Reflection;Read Embed Text버튼을 마우스 오른쪽 버튼으로 클릭하여 이벤트를 생성하면 다음과 같이 버튼 코드를 작성합니다.
assembly라는Assembly메서드 개체를 만듭니다.var asmbly = Assembly.GetExecutingAssembly();그런 다음
filepath라는var유형 변수를 만듭니다. 이 변수는 텍스트 파일 경로를 보유합니다.var filePath = "ReadEmbedTextbyZeeshan.files.Shani.txt";마지막으로 주어진 경로에서 텍스트 파일을 읽는
StreamReader를 사용하여 포함된 파일 텍스트를 표시합니다.using (Stream s = asmbly.GetManifestResourceStream(filePath)) using ( StreamReader sr = new StreamReader(s)) { txtTextBox.Text = sr.ReadToEnd(); } -
완전한 소스 코드
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Reflection; namespace ReadEmbedTextbyZeeshan { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnText_Click(object sender, EventArgs e) { var asmbly = Assembly.GetExecutingAssembly(); var filePath = "ReadEmbedTextbyZeeshan.files.Shani.txt"; using (Stream s = asmbly.GetManifestResourceStream(filePath)) using ( StreamReader sr = new StreamReader(s)) { txtTextBox.Text = sr.ReadToEnd(); } } } }Read Embed Text버튼을 클릭하면 아래 출력이 표시됩니다.출력:

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn