대화상자도 컨트롤이다.
파일 대화상자는 파일 열기 대화상자와 파일 저장 대화상자가 있다.
다음은 OpenFileDialog 컨트롤의 속성을 정리해놓은 것이다.
Filter 어떤 파일 확장자를 보여줄 것인지 지정하는 파일 필터 리스트
예) openDlg.Filter = "모든 파일 (*.*)|*.*";
FilterIndex 필터 리스트에서 디폴트로 사용할 파일 필터를 지정
예) openDlg.FilterIndex = 2;
InitialDirectory 파일 다이얼로그를 처음 띄웠을 때, 보여줄 시작 디렉토리를 지정
예) openDlg.InitialDirectory = "c:\\";
Title 파일 다이얼로그의 타이틀에 보여줄 문자열을 지정
예) openDlg.Title = "파일 열기";
FileName 대화상자에서 선택한 파일 이름을 알려준다.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory="C:\\";//시작 루트
ofd.Filter = "실행파일 (*.exe)|*.exe|txt files (*.txt)|*.txt|All files (*.*)|*.*";
ofd.FilterIndex =2;
ofd.RestoreDirectory = true ;
//ofd.ShowDialog(); // ofd.showDialog 가 if 문에도 나오면 두개다
if(ofd.ShowDialog() == DialogResult.OK)
MessageBox.Show(ofd.FileName);
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}