2016-06-23 4 views
-1

열린 텍스트 대화 상자가있는 텍스트 편집기 프로그램에 약간의 문제가 있습니다. 파일을 실제로 선택할 때 완벽하게 작동하지만 취소하면 취소됩니다.Save-file 대화 상자에서 예외가 발생합니다

openFileDialog1.ShowDialog(); 

if (saveFileDialog1.InitialDirectory.Equals(saveFileDialog1.RestoreDirectory)) 
{ 
    MessageBox.Show("Didnt make a selection"); 
} 
else 
{ 
    txtUI.Text = File.ReadAllText(openFileDialog1.FileName); 
} 
saveFileDialog1.FileName = "Please click on the file you want to open"; 

if-else 문을 사용해 보았지만 작동하지 않는 것 같습니다.

+0

같은 시도의 catch 블록에 코드를 넣어? – Destrif

답변

1

당신은 반환 값 ShowDialog() 확인해야합니다 : 사용자가 대화 상자를 취소하는 경우

if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    // do your stuff 
} 

을, 결과는 DialogResult.Cancel 될 것이다.

+0

고맙습니다. 도움을 받으실 수 있습니다. – exodus2017

0

예외 로그는이

try{ 
     if (saveFileDialog1.InitialDirectory.Equals(saveFileDialog1.RestoreDirectory)) 
     { 
     MessageBox.Show("Didnt make a selection"); 
    } 
    else 
    { 
     txtUI.Text = File.ReadAllText(openFileDialog1.FileName); 
    } 
    saveFileDialog1.FileName = "Please click on the file you want to open"; 
    }catch{}