How to fix MethodAccessException while reading a file?

I need to read a text file added in my project DataMid / Bigram_MidWord.txt, where DataMid is the folder and Bigram_MidWord.txt is the file to read. When I write an expression

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 problem?

+1
source share
1 answer

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

To access the text file, which is the 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(); 
+1
source

Source: https://habr.com/ru/post/914380/


All Articles