Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone从文本文件读取_C#_Windows Phone 7 - Fatal编程技术网

C# Windows Phone从文本文件读取

C# Windows Phone从文本文件读取,c#,windows-phone-7,C#,Windows Phone 7,我正在编写一个从文本文件读取数据的应用程序,并将其用作应用程序的基础。这只是一个简单的文本文件,包含程序需要的几行数据。我已经将文本文件作为项目的一部分包含在VisualStudio中。但是,当我尝试运行应用程序并使用StreamReader读取文本文件时,它会抛出一个错误: “System.MethodAccessException:安全透明方法”App.MainPage..ctor()“尝试访问安全关键方法”System.IO.File.Exists(System.String)”失败。 位

我正在编写一个从文本文件读取数据的应用程序,并将其用作应用程序的基础。这只是一个简单的文本文件,包含程序需要的几行数据。我已经将文本文件作为项目的一部分包含在VisualStudio中。但是,当我尝试运行应用程序并使用StreamReader读取文本文件时,它会抛出一个错误:

“System.MethodAccessException:安全透明方法”App.MainPage..ctor()“尝试访问安全关键方法”System.IO.File.Exists(System.String)”失败。 位于System.IO.File.Exists(字符串路径)”

此文本文件对应用程序的功能非常重要。当人们直接从应用程序中下载并阅读它时,我有没有办法将它包含在XAP中

您可以使用类访问Windows Phone应用程序中的文本文件。要阅读它,请打开一个新的
文件流
。然后,您可以使用该
FileStream
创建StreamReader或StreamWriter

下面的代码访问
IsolatedStorageFile
,并打开一个新的StreamReader来读取内容

        using (IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForApplication())
        {
            //To read
            using (StreamReader r = new StreamReader(f.OpenFile("settings.txt", FileMode.OpenOrCreate)))
            {
                string text = r.ReadToEnd();
            }

            //To write
            using (StreamWriter w = new StreamWriter(f.OpenFile("settings.txt", FileMode.Create)))
            {
                w.Write("Hello World");
            }
        }

以下是在wp7应用程序中从解决方案读取文本文件的解决方案

  • 在解决方案中复制文本文件

  • 右键单击->属性

  • 现在将buildaction设置为Resource

    System.IO.Stream src = Application.GetResourceStream(new Uri("solutionname;component/text file name", UriKind.Relative)).Stream;
                using (StreamReader sr = new StreamReader(src))
                  {
                     string text = sr.ReadToEnd();
                  }
    

  • 请分享一些代码。你是这样读取文件的吗?我一直在尝试使用StreamReader在常规Windows桌面应用程序上读取它。可能重复:干得好。干得好。如果有人想将其分配给Textblock,那么他/她应该添加以下内容:texBlock1.Text=Text;