如何在文件读取期间修复异常MethodAccessException? [英] How to fix exception MethodAccessException during file reading?
问题描述
我必须阅读添加到我的项目DataMid/Bigram_MidWord.txt中的文本文件,其中DataMid是文件夹,Bigram_MidWord.txt是要读取的文件. 当我写语句
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);
然后我得到如下异常:
Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess)
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)
如何解决此问题?
推荐答案
问题是您试图使用FileStream
访问应用程序XAP文件中嵌入的资源. FileStream
用于访问文件系统(例如,隔离存储).
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).
要访问XAP文件中作为资源的文本文件,可以使用以下代码:
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();
这篇关于如何在文件读取期间修复异常MethodAccessException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!