【问题标题】:Can't Create MemoryStream using Custom File as Resource using C#无法使用 C# 使用自定义文件作为资源创建 MemoryStream
【发布时间】:2019-07-20 12:41:41
【问题描述】:

我正在使用 C#,但无法使用资源创建流。该文件是一个名为“test.usr”的自定义文件,其中包含一个字符串。它的构建操作设置为无(不确定是否重要)。错误发布在下面。有谁知道我将如何纠正这个问题?

错误:'查找类似于 ..... 的本地化字符串无法从字符串转换为 int。'

MemoryStream certStream = new MemoryStream(Properties.Resources.test);

【问题讨论】:

  • 使用类似MemoryStream certStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("xxx.yyy.Resources.test.usr");的东西
  • @NaDeRStar 它有一条红色波浪线,上面写着“无法将 System.IO.Stream 隐式转换为 System.IO.MemoryStream”
  • @NaDeRStar MemoryStream certStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Resources.test.usr");

标签: c# stream resources memorystream


【解决方案1】:

Properties.Resources.teststringMemoryStream 没有接受字符串的构造函数。它可以接受一个字节数组。因此您可以将string 转换为字节数组:

MemoryStream certStream = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Resources.test));

【讨论】:

  • 我所做的另一个有效的选择是将资源的 FileType 从 String 更改为 Binary
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-27
  • 2021-08-19
相关资源
最近更新 更多