开发者

read file in c# .net

i m developing an app in windows phon开发者_如何学编程e 7 in which i need to read a text file. i write a code for that but when i debug that code, it gives an error "method access exception". same code is working in c# windows form app. i dont know what is the problem. plz suggest me for this and solve this problem. my code is like this:

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);
        }

Error occured at line: "line = sr.ReadLine();"


Are you trying to open a file on your actual computer in this line, from the WP7 emulator?

StreamReader sr = new StreamReader(@"D:\abc.txt");

You definitely won't be able to do this from the phone since applications are isolated from each other for security reasons.

If you need to read in a static text file, consider include it as part of your project and referencing it there instead, using the System.Windows.Application.GetResourceStream method:

var resource = System.Windows.Application.GetResourceStream(new Uri("textfile.txt", UriKind.Relative)

See this question for more info.


Just as most other modern mobile devices, applications running on WP7 are sandboxed for security and reliability reasons. This also means you can't do random I/O on the the way you'd do it in an unrestricted environment.

WP7 does provide sandboxed persistence using familiar constructs as directories and files, within what is known as Isolated Storage. Try to look up IsolatedStorageFile.GetUserStoreForApplication() and take it from there.

Update: An example can be found here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜