0

I am using C# and can't create a stream using a Resource. The file is a custom file called 'test.usr' which contains a string. The build action for it is set to None (not sure if that matters). The error is posted below. Does anyone know how I would correct this?

Error: 'Looks up a localized string similar to ..... Can't convert from string to int.'

MemoryStream certStream = new MemoryStream(Properties.Resources.test);
John Doe
  • 1,950
  • 9
  • 32
  • 53
  • 1
    https://stackoverflow.com/questions/10412401/how-to-read-an-embedded-resource-as-array-of-bytes-without-writing-it-to-disk – TheGeneral Feb 26 '19 at 21:41
  • Possible duplicate of [How do I generate a stream from a string?](https://stackoverflow.com/questions/1879395/how-do-i-generate-a-stream-from-a-string) – ste-fu Feb 26 '19 at 21:41
  • Use somthing like `MemoryStream certStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("xxx.yyy.Resources.test.usr");` – NaDeR Star Feb 26 '19 at 21:43
  • @NaDeRStar it has a red squiggly line that says 'Cannot implicitly convert System.IO.Stream to System.IO.MemoryStream' – John Doe Feb 26 '19 at 21:48
  • @NaDeRStar MemoryStream certStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Resources.test.usr"); – John Doe Feb 26 '19 at 21:48

1 Answers1

1

Properties.Resources.test is a string. MemoryStream does not have a constructor that accepts strings. It can accept an array of bytes though. So you can convert the string to an array of bytes:

MemoryStream certStream = new MemoryStream(Encoding.UTF8.GetBytes(Properties.Resources.test));
Ilian
  • 5,113
  • 1
  • 32
  • 41
  • One other option I did that worked was to change the FileType of the resource from String to Binary – John Doe Feb 27 '19 at 02:38