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)

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

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

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

Sample C#

public string ReadEmbeddedResourceString(string name)
{
	try
	{
		if (String.IsNullOrEmpty(name) || String.IsNullOrWhiteSpace(name)) { return string.Empty; }
		var assembly = Assembly.GetExecutingAssembly();
		using (var manifestResourceStream = assembly.GetManifestResourceStream(name))
		if (manifestResourceStream != null)
		{
			using (var streamReader = new StreamReader(manifestResourceStream))
			{
				return streamReader.ReadToEnd();
			}
		}
	}
	catch (Exception ex)
	{
		//handle the exception your way
	}
	return string.Empty;
}

Sample VB.NET

Public Function ReadEmbeddedResourceString(name As String) As String
	Try
		If String.IsNullOrEmpty(name) OrElse String.IsNullOrWhiteSpace(name) Then
			Return String.Empty
		End If
		Dim assembly__1 = Assembly.GetExecutingAssembly()
		Using manifestResourceStream = assembly__1.GetManifestResourceStream(name)
			If manifestResourceStream IsNot Nothing Then
				Using streamReader = New StreamReader(manifestResourceStream)
					Return streamReader.ReadToEnd()
				End Using
			End If
		End Using
	Catch ex As Exception
	        'handle the exception your way
        End Try
	Return String.Empty
End Function

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