How to read a embedded resource to String in C# and VB.NET

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#

Sample VB.NET (autogenerated)

Please rate this snippet

How to read a embedded resource image in C# and VB.NET

To read a embedded resource image in C# and VB.NET you can use the following snippet.

Sample C#

public static Image ReadEmbeddedRessourceImage(Assembly assembly, string searchPattern)
{
	var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(x => x.Contains(searchPattern));
	using (var stream = assembly.GetManifestResourceStream(resourceName))
	{
		if (stream != null) return Image.FromStream(stream);
	}
	return null;
}

Sample VB.NET

Public Shared Function ReadEmbeddedRessourceImage(assembly As Assembly, searchPattern As String) As Image
	Dim resourceName = assembly.GetManifestResourceNames().FirstOrDefault(Function(x) x.Contains(searchPattern))
	Using stream = assembly.GetManifestResourceStream(resourceName)
		If stream IsNot Nothing Then
			Return Image.FromStream(stream)
		End If
	End Using
	Return Nothing
End Function

Please rate this snippet

How to read multiple embedded ressource names with a search pattern in C# and VB.NET

To read multiple embedded ressources with a search pattern in C# and VB.NET you can use the following snippet.

Sample C#

public List<String> ReadEmbeddedRessources(string pattern)
{
	var res = new List<String>();
	try
	{
		var myAssembly = Assembly.GetExecutingAssembly();
		var resList = myAssembly.GetManifestResourceNames();
		res.AddRange(resList.Where(resListItem => resListItem.Contains(pattern)));
		return res;
	}
	catch (Exception ex)
	{
		//handle the exception your way
		return new List<String>();
	}
}

Sample VB.NET

Public Function ReadEmbeddedRessources(ByVal pattern As String) As List(Of String)
        Dim res As var = New List(Of String)
        Try 
            Dim myAssembly As var = Assembly.GetExecutingAssembly
            Dim resList As var = myAssembly.GetManifestResourceNames
            res.AddRange(resList.Where(() => {  }, resListItem.Contains(pattern)))
            Return res
        Catch ex As Exception
            'handle the exception your way
            Return New List(Of String)
        End Try
    End Function

Please rate this snippet