wpf - C# OpenFileDialog - exception when no files are chosen -


i trying work out on error, when no files chosen program goes next step although shouldn't. have tried:

if (filetocheck != null) 

but didn't work. other suggestions?

private void mail(object sender, routedeventargs e) {     openfiledialog openfiledialog = new openfiledialog();     openfiledialog.filter = "text files (*.txt)|*.txt|all files (*.*)|*.*";      if (openfiledialog.showdialog() == true)     {             spamtext.text = file.readalltext(openfiledialog.filename);                    }       string[] filetocheck = { openfiledialog.filename };      splitter(filetocheck);     mail = tempdict; } 

you on right track.

but checking if (filetocheck != null) not enough, since when no file selected, openfiledialog.filename contains empty string, not null.

so can use if (!string.isnullorempty(filetocheck)) check.

another way - put code around filetocheck outside of openfiledialog.showdialog() == true condition inside of it. looks more logical, since if file not selected, condition not hit , don't need proceed additional check.

so code like

if (openfiledialog.showdialog() == true) {     string filename = openfiledialog.filename;     if (!string.isnullorempty(filename) && file.exists(filename))     {         spamtext.text = file.readalltext(filename);                        splitter(new [] {filename});     } } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -