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

One thought on “How to read embedded resource string in C# and VB.NET

Leave a Reply