【问题标题】:how to get file from resources as a stream? (.net)如何从资源中获取文件作为流? (。网)
【发布时间】:2008-12-21 18:45:31
【问题描述】:

我在资源(xsd 文件)中有几个文件用于验证收到的 xml 消息。我使用的资源文件名为 AppResources.resx,它包含一个名为 clientModels.xsd 的文件。当我尝试使用这样的文件时:AppResources.clientModels,我得到一个包含文件内容的字符串。我想改为流。 我不希望使用 assembly.GetManifestResourceStream,因为我对它有过不好的体验(由于某种原因,使用这些流使用 SharpZipLib 归档文件不起作用)。 还有其他方法吗?我听说过 ResourceManager - 有什么可以帮助我的吗?

【问题讨论】:

    标签: c# .net resources


    【解决方案1】:

    您能否将输入的字符串输入 System.IO.StringReader,也许?这可能会做你想要的。您可能还想查看 MemoryStream。

    【讨论】:

      【解决方案2】:

      这是链接中的代码

      //Namespace reference
      using System;
      using System.Resources;
      
      
      #region ReadResourceFile
      /// <summary>
      /// method for reading a value from a resource file
      /// (.resx file)
      /// </summary>
      /// <param name="file">file to read from</param>
      /// <param name="key">key to get the value for</param>
      /// <returns>a string value</returns>
      public string ReadResourceValue(string file, string key)
      {
          //value for our return value
          string resourceValue = string.Empty;
          try
          {
              // specify your resource file name 
              string resourceFile = file;
              // get the path of your file
              string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
              // create a resource manager for reading from
              //the resx file
              ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
              // retrieve the value of the specified key
              resourceValue = resourceManager.GetString(key);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
              resourceValue = string.Empty;
          }
          return resourceValue;
      }
      #endregion
      

      它的来源不是我写的代码

      http://www.dreamincode.net/code/snippet1683.htm

      HTH

      骨头

      【讨论】:

        【解决方案3】:

        我有一个作为资源加载的 zip 文件,直接从命名空间引用它会给我字节,而不是字符串。在资源设计器中右键单击您的文件,然后将文件类型从文本更改为二进制。然后你会得到一个字节数组,你可以将它加载到 MemoryStream 中。

        【讨论】:

          猜你喜欢
          • 2012-12-01
          • 2013-09-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-12
          • 1970-01-01
          • 1970-01-01
          • 2019-11-08
          • 1970-01-01
          相关资源
          最近更新 更多