Windows phone 8 正在读取项目文本文件windows phone 8

Windows phone 8 正在读取项目文本文件windows phone 8,windows-phone-8,file-io,Windows Phone 8,File Io,访问与Windows Phone 8应用程序打包的文本文件时遇到问题 代码如下: var ResrouceStream = Application.GetResourceStream(new Uri("Data-Test.docx", UriKind.Relative)); if (ResrouceStream != null) { Stream myFileStream = ResrouceStream.Stream; if (myFileStream.CanRead)

访问与Windows Phone 8应用程序打包的文本文件时遇到问题

代码如下:

var ResrouceStream = Application.GetResourceStream(new Uri("Data-Test.docx", UriKind.Relative));
if (ResrouceStream != null)
{
    Stream myFileStream = ResrouceStream.Stream;
    if (myFileStream.CanRead)
    {
        // logiic here
        retrun "Hi";
    }
}
else
{
    return "hello";
}
看起来很简单,但应用程序总是返回“你好”。我已经将文件放在根目录下,也放在资产中,将其更改为内容-复制和不复制,资源复制和不复制,但它始终返回“hello”

我花了几个小时在这个和我能找到的所有解决方案上,展示了上面的解决方案或非常类似的解决方案

我做错了什么

编辑:当我部署到手机或模拟器时返回“hello”。 还尝试了“/数据测试…”、@“\Data Text…”、@/”数据测试

更新1:

         string aReturn = "";
         var asm = Assembly.GetExecutingAssembly();

         //Use this to verify the namespacing of the "Embedded Resource".
         //asm.GetManifestResourceNames()
         //               .ToList()
         //               .ForEach(name => Debug.WriteLine(name));

         var ResourceStream = asm.GetManifestResourceStream("ContosoSocial.Assets.QuizQuestions.QuizQuestions-Test1.docx");

         if (ResourceStream != null) //  <--CHECKED AND DOES NOT EQUAL NULL
         {
             Stream myFileStream = ResourceStream;

             if (myFileStream.CanRead) // <-- CHEACKED AND CAN READ
             {
                 StreamReader myStreamReader = new StreamReader(myFileStream);

                 LOGIC & EXCEPTION HERE...?
                 string myLine = myStreamReader.ReadLine();
             }
             else
             {
                 aReturn = "myFileStream.CanRead = true";
             }
         }
         else
         {
             aReturn = "stream equals null";
         }
         Debug.WriteLine(aReturn);
     }
…下面是

 string myLine = myStreamReader.ReadLine();
…并注意到它只检索了2个字符“PK”! 因此,将.docx文件保存为.txt并重新插入adn,并将构建副本更改为嵌入式-不要复制…快乐时光它现在删除了文件中的第一行

感谢OmegaMan为您提供的帮助:-)

  • 将项目中的文件类型更改为
    嵌入式资源
  • 通过将命名空间移动到其位置来提取资源。下面是我拉入XSD的示例代码:
  • 代码:


    注意,这并没有在WP8上测试,但GetExecutionGassembly声明在.Net中工作。如果名称空间有误,请取消对代码的注释,并显示或调试以确定资源及其名称空间。

    当我遇到一些红线错误时,请检查foreach循环中的语法。@TripVoltage My bad…感谢您指出这一点。在上面的示例中修复。谢谢,调试器输出是contosocial.g.resources contosocial.resources.AppResources.resources contosocial.QuizQuestions-Test.docx,然后我像这样分配它:var ResourceStream=asm.GetManifestResourceStream(“contosocial.QuizQuestions.docx”);但当我检查它时,它总是空的@TripVoltage是的,您只对.docx文件感兴趣,这样就可以使用.scrap,我现在使用这个文件contosocial.Assets.QuizQuestions.QuizQuestions-Test1.docx。。。干杯伙计非常感谢:-)您的文件中有什么构建操作?@csharpwinphonexaml已嵌入,请勿复制
     string myLine = myStreamReader.ReadLine();
    
    var asm = Assembly.GetExecutingAssembly();
    
    // Use this to verify the namespacing of the "Embedded Resource".
    //  asm.GetManifestResourceNames()
    //     .ToList()
    //     .ForEach(name => Debug.WriteLine(name));
    
    var f1 = asm.GetManifestResourceStream("UnitTests.Resources.NexusResponse.xsd");