Search code examples
c#streamresourcesmemorystream

Can't Create MemoryStream using Custom File as Resource using C#


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);

Solution

  • 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));