use*_*199 3 c# windows-phone-7
我在Windows Phone 7中开发一个应用程序,我需要在其中读取文本文件.我为此编写代码但是当我调试该代码时,它会给出错误"方法访问异常".相同的代码在c#windows form app中工作.我不知道是什么问题.PLZ建议我这样做并解决这个问题.我的代码是这样的:
namespace fileread
{
public partial class MainPage : PhoneApplicationPage
{
private string line;
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
StreamReader sr = new StreamReader(@"D:\abc.txt");
line = sr.ReadLine();
MessageBox.Show(line);
}
Run Code Online (Sandbox Code Playgroud)
在行发生错误:"line = sr.ReadLine();"
您是否尝试从WP7仿真器中在此行中的实际计算机上打开文件?
StreamReader sr = new StreamReader(@"D:\abc.txt");
Run Code Online (Sandbox Code Playgroud)
您绝对无法通过手机执行此操作,因为出于安全原因,应用程序彼此隔离.
如果需要读取静态文本文件,请考虑将其作为项目的一部分包含在内,并使用System.Windows.Application.GetResourceStream方法在那里引用它:
var resource = System.Windows.Application.GetResourceStream(new Uri("textfile.txt", UriKind.Relative)
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅此问题.