728x90
반응형

UI 개선하기

이전 글까지 파일 관리 앱의 핵심 기능을 모두 구현했습니다.
이번에는 앱의 디자인(UI)을 개선해서 더 깔끔하고 사용하기 쉽게 만들어보겠습니다!


1. 최종 목표

  • 버튼 배치 정리
  • 입력창과 버튼 정렬
  • ProgressBar 위치 조정
  • 레이블(Label) 추가해 입력창 설명

2. UI 구성 정리

 기본 레이아웃 제안

영역 구성

상단 폴더 선택 버튼 + 폴더 경로 표시 (TextBox)
중단 검색창(TextBox) + 검색 버튼
좌측 파일 리스트(ListBox, 다중 선택 가능)
우측 복사, 이동, 삭제, 이름 변경 버튼 세로 배치
하단 진행률 ProgressBar + 현재 작업 상태 표시

3. UI 개선 방법

 그룹박스(GroupBox) 활용

  • 관련된 버튼끼리 묶어서 정리
  • 예) 파일 작업 그룹 (복사/이동/삭제/이름 변경 버튼)

 Label 추가 예시

Label lblFolder = new Label();
lblFolder.Text = "폴더 경로:";
lblFolder.Location = new Point(10, 10);
this.Controls.Add(lblFolder);

Label lblSearch = new Label();
lblSearch.Text = "파일 검색:";
lblSearch.Location = new Point(10, 60);
this.Controls.Add(lblSearch);

 버튼 정렬하기

  • 복사/이동/삭제/이름 변경 버튼을 세로로 깔끔하게 배치
  • Anchor 속성 사용해서 창 크기에 따라 자동 정렬 유지
btnBatchCopy.Anchor = AnchorStyles.Top | AnchorStyles.Right;
btnBatchMove.Anchor = AnchorStyles.Top | AnchorStyles.Right;
btnDeleteFile.Anchor = AnchorStyles.Top | AnchorStyles.Right;
btnRenameFile.Anchor = AnchorStyles.Top | AnchorStyles.Right;

 ProgressBar 위치

  • 폼 맨 하단에 가로로 꽉 차게 배치
  • 진행률 외에 현재 작업 상태 메시지도 Label로 표시하면 더 좋음
progressBar.Dock = DockStyle.Bottom;
Label lblStatus = new Label();
lblStatus.Text = "대기 중";
lblStatus.Dock = DockStyle.Bottom;

4. 최종 앱 화면 구성 예시

┌───────────────────────────────────────────────┐
│ [폴더 선택] [텍스트박스: 폴더경로]             │
│ [파일 검색] [텍스트박스] [검색 버튼]            │
├───────────────────────────────────────────────┤
│ [파일 리스트 (ListBox)]  │ [버튼 그룹 (복사/이동/삭제/이름 변경)] │
├───────────────────────────────────────────────┤
│ [진행률 ProgressBar] + [현재 상태 Label]       │
└───────────────────────────────────────────────┘

 

이번 글에서는 WinForms 파일 관리 앱의 UI를 정리하고 개선하는 방법을 알아봤습니다.

  • 입력창/버튼/리스트 정렬
  • Label과 GroupBox로 가독성 향상
  • ProgressBar 위치와 상태 표시 추가

이제 이 파일 관리 앱은 기능과 디자인 모두 갖춘 미니 프로그램이 되었습니다!

 

728x90
반응형
728x90
반응형

이동 기능과 진행률 표시 추가하기

이전 글에서는 파일 목록 불러오기, 복사, 삭제 기능을 가진 기본 파일 관리 앱을 만들었습니다.
이번에는 기능을 더 확장해서 파일 이동(Move) 기능을 추가하고, 복사/이동 시 진행률(ProgressBar) 도 표시해보겠습니다!


1. 추가 목표

  • 파일 이동(Move) 기능 구현
  • 복사/이동할 때 진행 상황을 ProgressBar로 표시

2. UI 요소 추가

  • ProgressBar (Name: progressBar) - 초기 Visible: false, Minimum: 0, Maximum: 100
  • Button (Name: btnMoveFile, Text: "이동") 추가

3. 코드 수정 및 추가

 이동 기능 추가

private async void btnMoveFile_Click(object sender, EventArgs e)
{
    string folderPath = txtFolderPath.Text;
    string fileName = txtFileName.Text;

    string sourcePath = Path.Combine(folderPath, fileName);
    string destPath = Path.Combine(folderPath, "이동됨_" + fileName);

    if (!File.Exists(sourcePath))
    {
        MessageBox.Show("파일이 존재하지 않습니다.");
        return;
    }

    progressBar.Visible = true;
    progressBar.Value = 0;

    await MoveFileWithProgressAsync(sourcePath, destPath);

    MessageBox.Show("파일 이동 완료!");
    progressBar.Visible = false;
    btnLoadFiles.PerformClick();
}

private async Task MoveFileWithProgressAsync(string sourcePath, string destPath)
{
    const int bufferSize = 81920; // 80KB 버퍼 크기
    byte[] buffer = new byte[bufferSize];

    FileInfo fileInfo = new FileInfo(sourcePath);
    long totalBytes = fileInfo.Length;
    long readBytes = 0;

    using (FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
    using (FileStream destStream = new FileStream(destPath, FileMode.Create, FileAccess.Write))
    {
        int bytesRead;
        while ((bytesRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
        {
            await destStream.WriteAsync(buffer, 0, bytesRead);
            readBytes += bytesRead;

            int progress = (int)((readBytes * 100) / totalBytes);
            progressBar.Value = Math.Min(progress, 100);
        }
    }

    File.Delete(sourcePath); // 복사 완료 후 원본 삭제
}

4. 주요 포인트 정리

항목 설명

파일 이동 구현 직접 복사한 후 원본 삭제 방식으로 구현
진행률 표시 복사 진행 중 읽은 바이트 수로 퍼센트 계산
ProgressBar 제어 Visible 속성으로 시작/완료 시 표시/숨김
await 활용 UI 멈춤 없이 부드럽게 동작

5. 전체 흐름 요약

  1. 사용자가 파일 선택 → 이동 버튼 클릭
  2. ProgressBar를 보여주며 파일을 복사
  3. 복사가 끝나면 원본 파일 삭제 (실제 이동 효과)
  4. 이동 완료 메시지 + 파일 목록 새로고침

이번 글에서는

  • 파일 이동 기능 추가
  • 복사/이동 시 진행률 표시(ProgressBar) 를 통해 파일 관리 앱을 더욱 실용적으로 확장했습니다.

비동기 작업 + 사용자 경험 개선(UI 피드백) 을 동시에 연습해볼 수 있었습니다!

 

728x90
반응형
728x90
반응형

GUI 기초 프로젝트

지난 글에서는 콘솔 기반 파일 관리 도구를 만들었습니다.
이번에는 C# WinForms를 이용해 GUI(그래픽 사용자 인터페이스) 형태로 파일 관리 앱을 만들어보겠습니다!

WinForms는 데스크탑 앱을 간단하게 만들 수 있는 프레임워크로, 초보자에게 매우 친숙합니다.


1. 프로젝트 목표

  • 폴더 경로를 입력 받아 파일 목록 출력
  • 파일 복사 버튼
  • 파일 삭제 버튼

2. 기본 설계

 폼 구성

  • TextBox: 폴더 경로 입력
  • Button: 파일 목록 새로고침
  • ListBox: 파일 목록 표시
  • TextBox: 선택된 파일 이름 입력
  • Button: 파일 복사
  • Button: 파일 삭제

3. 코드 작성하기

 폼 디자이너 예시

(Visual Studio에서 끌어다 놓기만 하면 됩니다)

  • TextBox (Name: txtFolderPath)
  • Button (Name: btnLoadFiles, Text: "파일 불러오기")
  • ListBox (Name: listBoxFiles)
  • TextBox (Name: txtFileName)
  • Button (Name: btnCopyFile, Text: "복사")
  • Button (Name: btnDeleteFile, Text: "삭제")

 코드 예제

using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void btnLoadFiles_Click(object sender, EventArgs e)
    {
        listBoxFiles.Items.Clear();
        string folderPath = txtFolderPath.Text;

        if (!Directory.Exists(folderPath))
        {
            MessageBox.Show("폴더가 존재하지 않습니다.");
            return;
        }

        string[] files = Directory.GetFiles(folderPath);
        listBoxFiles.Items.AddRange(files);
    }

    private async void btnCopyFile_Click(object sender, EventArgs e)
    {
        string folderPath = txtFolderPath.Text;
        string fileName = txtFileName.Text;

        string sourcePath = Path.Combine(folderPath, fileName);
        string destPath = Path.Combine(folderPath, "복사본_" + fileName);

        if (!File.Exists(sourcePath))
        {
            MessageBox.Show("파일이 존재하지 않습니다.");
            return;
        }

        using (FileStream sourceStream = File.Open(sourcePath, FileMode.Open))
        using (FileStream destStream = File.Create(destPath))
        {
            await sourceStream.CopyToAsync(destStream);
        }

        MessageBox.Show("파일 복사 완료!");
        btnLoadFiles.PerformClick(); // 목록 새로고침
    }

    private void btnDeleteFile_Click(object sender, EventArgs e)
    {
        string folderPath = txtFolderPath.Text;
        string fileName = txtFileName.Text;
        string filePath = Path.Combine(folderPath, fileName);

        if (!File.Exists(filePath))
        {
            MessageBox.Show("파일이 존재하지 않습니다.");
            return;
        }

        File.Delete(filePath);
        MessageBox.Show("파일 삭제 완료!");
        btnLoadFiles.PerformClick(); // 목록 새로고침
    }
}

5. 주요 포인트

항목 설명

파일 목록 표시 ListBox.Items.AddRange() 사용
비동기 복사 CopyToAsync로 복사 중에도 UI 멈추지 않게 함
버튼 이벤트 연결 Designer 창에서 각 버튼의 Click 이벤트 연결
사용자 입력 검증 파일 존재 여부를 항상 체크

 마무리

이번 글에서는 C# WinForms를 사용해 기본적인 파일 관리 GUI 앱을 만들어봤습니다.

  • 폴더 탐색
  • 파일 복사
  • 파일 삭제
  • 비동기 처리로 UI 멈춤 방지

WinForms는 초보자도 빠르게 앱을 만들어 볼 수 있어 C# 학습에 매우 좋은 선택지입니다!

 

728x90
반응형

+ Recent posts