Silverlight 打开文件时避免出现Security Exception的办法

一般情况下写法:

 

 ofd = new OpenFileDialog(); ofd.Filter = "(PDF文件)|*.pdf|(Excel2003文件)|*.xls|(Excel2007文件)|*.xlsx"; if (ofd.ShowDialog() == true) { // if (!string.IsNullOrEmpty(ofd.File.ToString().Trim())) { } }

 

Silverlight4下无法用断点调试OpenFileDialog(),一旦有断点,就会抛出SecurityException异常;

 

比较好的写法:

 

private void btOpen_Click(object sender, RoutedEventArgs e) { ofd = new OpenFileDialog(); ofd.Filter = "(TXT文件)|*.txt"; if (ofd.ShowDialog()??false) { using (var reader = ofd.File.OpenText()) { string strLine = string.Empty; while ((strLine = reader.ReadLine()) != null) { MessageBox.Show(strLine.Trim()); } } } }

 

 

你可能感兴趣的:(exception,object,String,Excel,Security,silverlight)