Yesterday, the company happened to use savefiledialog () to export XML to the hard disk. We accidentally selected a directory with only the read permission. After prompting that there is no write permission, after a while, an unhandled exception is thrown, and the entire page is blank.CodeAfter that, I changed a way of writing, and the result would be the same with the exception, so I thought it should be a Silverlight bug. Because I was using Silverlight 4 Beta, I was not sure whether it was just a Server Load balancer bug.
When I got home, I started debugging the problem again.ProgramThis time, Silverlight 3.0 is used. The test environment is Windows XP, win2003, and win7. The Code is as follows:
-
-
Private Void Button#click ( Object Sender, routedeventargs e) {savefiledialog savefiledlg = New Savefiledialog (); If (Savefiledlg. showdialog (). Value ){ Try { // If you try a directory that does not have the write permission (such as the LAN shared directory), win2003 or XP will immediately encounter an exception Using (System. Io. Stream FS = Savefiledlg. openfile ()){}} Catch (Exception ex) {MessageBox. Show (ex. Message );}}}
Open a directory without the write permission, enter the file name, win2003 or XP on the savefiledlg. an exception occurs in openfile () and the exception is caught. However, an uncaptured exception will occur after a while (this time is not fixed and the time is fast and slow, sometimes you need to click another control on the page to see it.) In WINXP, for example (the first one is MessageBox. show (ex. message )):
In win2003, for example (the first one is MessageBox. Show (ex. Message )):
The file stream. Finalize () that has not caught the exception is thrown when the FS is disabled,
-
Using(System. Io. Stream FS=Savefiledlg. openfile ()){}
So I changed the code
-
-
Savefiledialog savefiledlg = New Savefiledialog (); system. Io. Stream FS = Null ; If (Savefiledlg. showdialog (). Value ){ Try { // If you try a directory that does not have the write permission (such as the LAN shared directory), win2003 or XP will immediately encounter an exception FS = Savefiledlg. openfile ();} Catch (Exception ex) {MessageBox. Show (ex. Message );} Finally { // Savefiledlg. openfile () does not execute fs. Close () after an exception, because FS = NULL If (FS ! = Null ){ Try {Fs. Close ();} Catch (Exception ex) {MessageBox. Show (ex. Message );}}}}
The result is the same as the exception. Because the savefiledlg. openfile () exception occurs, FS. Close () will not be executed at all, and FS is null at all.
So how can we capture this uncaptured exception? Will it be possible to try... catch before savefiledlg. showdialog () in this case? So I changed the code
-
-
Savefiledialog savefiledlg = New Savefiledialog (); Try { If (Savefiledlg. showdialog (). Value ){ Try { // If you try a directory that does not have the write permission (such as the LAN shared directory), win2003 or XP will immediately encounter an exception Using (System. Io. Stream FS = Savefiledlg. openfile ()){}} Catch (Exception ex) {MessageBox. Show (ex. Message );}}} Catch (Exception ex) {MessageBox. Show (ex. Message );}
The results are the same, so the exception cannot be caught.
So far, I think this is a Silverlight 3.0 bug, which has not been solved in 4.0 Beta, because I have not mentioned another test environment, that is, under win7, when you try a directory that has no write permission, the "savefiledialog" button does not close the "dialog" button, but is displayed.
Unless you select another directory with the write permission or cancel the operation, the savefiledlg. showdialog () operation will not be returned, and no exception will occur.
I wonder if anyone is interested? Run the copy code ~~~~