I needed a method to load a resource file from a c# project, but I didn't want to specify the full path to the file. I want to be able to just specify the file name (assuming it's unique).
Loading a resource file into a string
public static class GetResource
public static string TextFile(string fileName)
var names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
var resourceName = names.FirstOrDefault(x => x.EndsWith(fileName));
var assembly = Assembly.GetExecutingAssembly();
var result = string.Empty;
using (var stream = assembly.GetManifestResourceStream(resourceName))
if (stream == null) return result;
using (var reader = new StreamReader(stream))
result = reader.ReadToEnd();
var resourceText1 = GetResource.TextFile("fileName.txt");
var resourceText2 = GetResource.TextFile("files.fileName.txt");
var resourceText2 = GetResource.TextFile("project.files.fileName.txt");
Pete's Code Library
My code library is where I keep all my useful bits of code that I refer to over and again.
They are generally incomplete and are a quick tool to remind me of how to resolve an issue.