※ 화면에서 파일을 열거나 저장할 때 사용한다.
     자주 사용하는데 이상하게 기억이 안나는 경우가 있다.

 

<열기>

using System.Windows.Forms;


try
{
    // OpenFileDialog 생성
    OpenFileDialog ofd = new OpenFileDialog();

    // 처음 최초경로 설정
    ofd.InitialDirectory = @"C:\";

    // OpenFileDialog 창이름 설정
    ofd.Title = "열기";

    // OpenFileDialog 기본 파일이름 설정
    ofd.FileName = "test";

    // OpenFileDialog 파일종류 필터
    ofd.Filter = "이미지 파일 (*.jpg, *.jpge, *.gif, *.bmp, *.png) | *.jpg; *.jpge; *.gif; *.bmp; *png; | 모든 파일 (*.*) | *.*";

    // 파일 다중선택여부
    ofd.Multiselect = true;

    // OpenFileDialog 열기
    ofd.ShowDialog();
    
    // 제대로 파일을 가져왔는지 확인
    if (ofd.FileName == "")
    {
        return is_return;
    }
	
    // 경로 + 파일이름
    string strFileInfo = ofd.FileName;
    
    // 파일이름 + 확장자
    string strFileNmaeExt = ofd.SafeFileName;
    
    // 파일이름
    string strFilePath = Path.GetFileName(ofd.FileName);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "에러", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

 

 


<저장>

try
{
    // SaveFileDialog 생성
    SaveFileDialog sfd = new SaveFileDialog();

    // 처음 최초경로 설정
    sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

    // 창이름 설정
    sfd.Title = "다운로드";

    // 기본 파일이름 설정
    sfd.FileName = "테스트저장_" + DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.ToString("HHmmss");

    // 파일종류 필터
    sfd.Filter = "Excel 통합 문서|*.xls";

    //  확장명을 생략한 경우 대화 상자가 파일 이름에 확장명을 자동으로 추가할지를 나타내는 값을 가져오거나 설정
    sfd.AddExtension = true;

    // OK버튼을 눌렀는지 확인
    if (sfd.ShowDialog() == DialogResult.OK)
    {
        // 서버에서 받아오는 등 다운로드 처리
        //...
        //...

        MessageBox.Show("저장이 완료되었습니다.", "저장", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else
    {
        MessageBox.Show("저장이 취소되었습니다.", "저장", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
catch (Exception ex)
{
    MessageBox.Show("저장 중 에러가 발생하였습니다.", "저장", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

 

'C#' 카테고리의 다른 글

[C#] CSV형태의 레포트 만들기2(단일 데이터)  (0) 2025.02.18
[C#] RadioButton CheckedChanged 중복실행  (0) 2025.02.16
[C#] DateTimePicker 값 설정  (0) 2025.02.13
[C#] IronOCR 사용후기  (1) 2025.02.12
[C#] WebService 만들기  (0) 2025.02.10

+ Recent posts