C#

OpenFileDialog, FolderBrowserDialog

전라남도교육지원청 2022. 5. 5. 14:03

외부 파일을 불러오거나 폴더를 지정하기 위해 사용할 수 있는 클래스

string path = string.Empty;                     // 파일 경로를 저장할 string
OpenFileDialog dialog = new OpenFileDialog();   // 인스턴스화
dialog.InitialDirectory = @"C:";                // 초기 경로 지정
dialog.Filter = 
    "bmp files (*.bmp)|*.bmp|gif files (*.gif)|*.gif|All files (*.*)|*.*";
                                                // 파일 필터 설정 |로 구분
dialog.FilterIndex = 1;                         // 필터 인덱스 설정
dialog.RestoreDirectory = false;                // 마지막 탐색 경로 저장 여부
if (dialog.ShowDialog() == DialogResult.OK)     // 대화창에서 '확인'을 눌렀으면 실행
{
    path = dialog.FileName;                     // 선택한 파일의 경로를 path에 저장
}
else
{
    return;                                     // 대화창 취소 누르면 return
}
string path = string.Empty;
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
    path = dialog.SelectedPath + "\\partition";     // 선택한 경로를 path에 저장
    if (!System.IO.Directory.Exists(path))          // 경로가 존재하는지 확인
        System.IO.Directory.CreateDirectory(path);  // 폴더 생성
}
else
{
    return;
}