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

Please rate this snippet