今天我花了一些时间阅读一些类似的帖子来解决这个问题。在Release模式下运行是可以的——没有这样的错误或问题。但在Debug模式下,我无法打开任何文本文件。注意,我在这里找到并阅读了一篇文章,指出我正在以管理员模式运行Visual Studio 2015。但我无法确定问题所在。有人能够指导我正确的方向吗?
来自catch的Debug输出:
异常抛出:'System.UnauthorizedAccessException' in mscorlib.dll
文件无法读取:
拒绝访问路径'D:\TestFiles\Basic TextFiles\TextFile.txt'.
来自catch的Debug输出:
异常抛出:'System.UnauthorizedAccessException' in mscorlib.dll
文件无法读取:
拒绝访问路径'D:\TestFiles\Basic TextFiles\TextFile.txt'.
private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|CSharp (*.cs)|*.cs|XAML Format (*.xaml)|*.xaml|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
try
{
using (Stream sr = new FileStream(dlg.FileName, FileMode.Open))
{
TextRange range = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd);
range.Load(sr, DataFormats.Rtf);
}
}
catch (Exception ex)
{
Debug.WriteLine("The file could not be read:");
Debug.WriteLine(ex.Message);
}
}
}
D:\TestFiles\Basic TextFiles\TextFile.txt
吗? - Hari PrasadDebug.WriteLine
仅在调试模式下工作,所以你看不到异常。 - Hari Prasad