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#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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)
1 2 3 4 5 6 7 8 9 10 11 |
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 |