본문 바로가기
Study/C#

C# Windows Forms 강의 23편: 사용자 설정(Configuration) 저장 및 로드

by wawManager 2025. 2. 26.
728x90

1. 강의 개요

이번 강의에서는 Windows Forms 애플리케이션에서 사용자 설정을 저장하고 다시 불러오는 방법을 학습합니다.

  • Application Settings: 애플리케이션 설정을 저장하기 위한 .NET의 기본 메커니즘.
  • 설정은 애플리케이션이 종료된 후에도 유지되며, 다시 실행될 때 복원됩니다.

2. 학습 목표

  1. Application Settings를 사용해 사용자 설정(예: 폼 크기, 텍스트박스 값 등)을 저장.
  2. 애플리케이션 시작 시 설정을 불러와 UI에 적용.
  3. 설정 저장 버튼을 통해 사용자 정의 값을 저장.

3. Application Settings란?

Application Settings는 애플리케이션의 사용자 설정을 저장 및 관리하기 위한 .NET Framework의 기능입니다.

  • 설정은 **config 파일(App.config)**에 저장됩니다.
  • Windows Forms에서는 간단히 Properties.Settings.Default를 사용하여 값을 저장하고 불러올 수 있습니다.

Application Settings 주요 메서드

메서드 설명 예제

Save() 변경된 설정 값을 저장 Properties.Settings.Default.Save();
Reload() 저장된 설정 값을 다시 로드 Properties.Settings.Default.Reload();

4. 실습: 사용자 설정 저장 및 로드 구현

요구사항

  1. TextBox에 입력된 값을 설정으로 저장하고, 애플리케이션 실행 시 불러오기.
  2. 폼 크기(Width, Height)를 저장하고, 종료 후 다시 실행했을 때 복원.
  3. "설정 저장" 버튼 클릭 시 현재 설정 저장.

폼 구성

컨트롤 타입 이름 텍스트 위치 크기

Label lblMessage "메시지:" (20, 20) (50 x 20)
TextBox txtMessage (없음) (100, 20) (150 x 20)
Button btnSaveSettings "설정 저장" (20, 60) (100 x 30)

코드 작성

1. 설정 추가

  1. 솔루션 탐색기 → PropertiesSettings.settings 파일 열기.
  2. 아래와 같이 설정 항목 추가:

이름 형식 범위 기본값

Message string User (빈 값)
FormWidth int User 800
FormHeight int User 600

2. Form1.cs

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadSettings();
        }

        private void LoadSettings()
        {
            // 폼 크기 설정 불러오기
            this.Width = Properties.Settings.Default.FormWidth;
            this.Height = Properties.Settings.Default.FormHeight;

            // 메시지 설정 불러오기
            txtMessage.Text = Properties.Settings.Default.Message;
        }

        private void BtnSaveSettings_Click(object sender, EventArgs e)
        {
            // 현재 설정 저장
            Properties.Settings.Default.FormWidth = this.Width;
            Properties.Settings.Default.FormHeight = this.Height;
            Properties.Settings.Default.Message = txtMessage.Text;

            // 설정 저장
            Properties.Settings.Default.Save();

            MessageBox.Show("설정이 저장되었습니다!", "알림");
        }

        // 폼 닫힐 때 현재 크기 저장
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            Properties.Settings.Default.FormWidth = this.Width;
            Properties.Settings.Default.FormHeight = this.Height;
            Properties.Settings.Default.Save();
            base.OnFormClosing(e);
        }
    }
}

3. Form1.Designer.cs

namespace WindowsFormsApp1
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;
        private Label lblMessage;
        private TextBox txtMessage;
        private Button btnSaveSettings;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.lblMessage = new Label();
            this.txtMessage = new TextBox();
            this.btnSaveSettings = new Button();
            this.SuspendLayout();

            // lblMessage
            this.lblMessage.AutoSize = true;
            this.lblMessage.Location = new System.Drawing.Point(20, 20);
            this.lblMessage.Name = "lblMessage";
            this.lblMessage.Size = new System.Drawing.Size(50, 20);
            this.lblMessage.TabIndex = 0;
            this.lblMessage.Text = "메시지:";

            // txtMessage
            this.txtMessage.Location = new System.Drawing.Point(100, 20);
            this.txtMessage.Name = "txtMessage";
            this.txtMessage.Size = new System.Drawing.Size(150, 27);
            this.txtMessage.TabIndex = 1;

            // btnSaveSettings
            this.btnSaveSettings.Location = new System.Drawing.Point(20, 60);
            this.btnSaveSettings.Name = "btnSaveSettings";
            this.btnSaveSettings.Size = new System.Drawing.Size(100, 30);
            this.btnSaveSettings.TabIndex = 2;
            this.btnSaveSettings.Text = "설정 저장";
            this.btnSaveSettings.UseVisualStyleBackColor = true;
            this.btnSaveSettings.Click += new System.EventHandler(this.BtnSaveSettings_Click);

            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(300, 150);
            this.Controls.Add(this.btnSaveSettings);
            this.Controls.Add(this.txtMessage);
            this.Controls.Add(this.lblMessage);
            this.Name = "Form1";
            this.Text = "설정 저장 예제";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
    }
}

6. 실행 결과

  1. 설정 불러오기
    • 애플리케이션 시작 시 이전에 저장한 Message 값과 폼 크기(Width, Height)가 로드됩니다.
    • 텍스트박스에 저장된 메시지가 표시됩니다.
  2. 설정 저장
    • 텍스트박스에 메시지를 입력 후 "설정 저장" 버튼 클릭 → Message, FormWidth, FormHeight 값이 저장됩니다.
    • 애플리케이션 종료 후 다시 실행하면 저장된 설정이 복원됩니다.
  3. 폼 크기 저장
    • 폼 크기를 조정한 후 닫으면, 다음 실행 시 이전 크기가 복원됩니다.

7. 주요 개념 요약

  1. Application Settings는 간단히 사용자 설정을 저장 및 로드할 수 있는 기능.
  2. Properties.Settings.Default를 사용해 설정 값을 읽고 쓰며, Save()로 저장.
  3. 설정 값은 애플리케이션 종료 후에도 유지되며, 재실행 시 복원 가능.

 

728x90