How to fix exception MethodAccessException during file reading?
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 开发者_StackOverflow中文版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?
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();
精彩评论