How to fix exception MethodAccessException during file reading?

余生颓废 提交于 2019-12-23 00:46:20

问题


I have to read a text file which added in my project DataMid/Bigram_MidWord.txt where DataMid is a folder and Bigram_MidWord.txt is a file to read. When i write the statement

FileStream fileStream = new FileStream(@"/SourceCode,component/DataMid/Bigram_MidWord.txt", FileMode.Open, FileAccess.ReadWrite);

then i get the exception as follows:
Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess)

How can I fix this issue?


回答1:


The problem is that you are trying to use FileStream to access a resource that is embedded in your application's XAP file. FileStream is used for accessing the file system (e.g. isolated storage).

To access a text file that is a resource in your XAP file, you can use the following code:

string text;
Uri uri = new Uri("("/AssembyName;component/DataMid/Bigram_MidWord.txt", UriKind.RelativeOrAbsolute);
StreamResourceInfo sri = App.GetResourceStream(uri);
StreamReader sr = new StreamReader(sri.Stream);
text= sr.ReadToEnd();
sr.Close();


来源:https://stackoverflow.com/questions/5458663/how-to-fix-exception-methodaccessexception-during-file-reading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!