기초 메모지

C# - 내장 리소스(Embedded Resource) 본문

Languages/C#

C# - 내장 리소스(Embedded Resource)

라큐브 2022. 6. 29. 23:38

프로그램에 환경설정 파일을 포함하여 값을 가져올 수 있지 않을까?

찾아본 결과 내장 리소스를 이용하면 된다.

 

1. 프로젝트 안에 내장 리소스로 사용할 파일을 추가합니다.

2. 리소스 파일의 속성 메뉴에서 빌드 작업을 "포함 리소스" 로 설정합니다.

3. 내장 리소스 값을 가져오기 위해 다음 코드를 입력합니다.

try
{
	// 실행 중인 리소스 어셈블리를 가져옵니다.
	Assemblys = Assembly.GetExecutingAssembly();
	// 실행 어셈블리에서 매니페스트 리소스를 조사하여 리소스 파일명을 찾습니다.
	string[] resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
	// resourceNames 배열에서 해당 파일 읽습니다.
    	using (SR = new StreamReader(Assemblys.GetManifestResourceStream(resourceNames[1])))
    	{
     	   textbox_data.Text = SR.ReadToEnd();
    	}
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

4. 버튼 이벤트로 내장 리소스에 있는 데이터를 읽어 폼에 표시할 수 있습니다.

 

반응형
Comments