To read a embedded resource to String in C# and VB.NET you can use the snippet below.
Keep in mind that this snippet is using Encoding.Default which might not always work in all situations correctly.

Sample C#

public string ReadEmbeddedRessourceToString(Assembly assembly, string searchPattern)
{
	var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.Contains(searchPattern));
	using (var stream = assembly.GetManifestResourceStream(resourceName))
	{
		if (stream != null)
		{
			using (var reader = new StreamReader(stream,Encoding.Default))
			{
				return reader.ReadToEnd();
			}
		}
	}
	return string.Empty;
}

Sample VB.NET (autogenerated)

Public Function ReadEmbeddedRessourceToString(assembly As Assembly, searchPattern As String) As String
	Dim resourceName = assembly.GetManifestResourceNames().FirstOrDefault(Function(x) x.Contains(searchPattern))
	Using stream = assembly.GetManifestResourceStream(resourceName)
		If stream IsNot Nothing Then
			Using reader = New StreamReader(stream,Encoding.Default)
				Return reader.ReadToEnd()
			End Using
		End If
	End Using
	Return String.Empty
End Function

Leave a Reply