Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created June 12, 2014 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemrekeskin/7589f6f3995740e16c05 to your computer and use it in GitHub Desktop.
Save yemrekeskin/7589f6f3995740e16c05 to your computer and use it in GitHub Desktop.
Resource file manager in assembly
public static class EmbeddedStringResources
{
static Dictionary<string, string> stringResources = new Dictionary<string, string>();
public static string LoadResource(string name)
{
// Tüm resource .dll içinde olmalı (GetExecutingAssembly())
Assembly assembly = Assembly.GetExecutingAssembly();
string resourceName = assembly.GetName().Name + ".Resources." + name;
string text;
if (!stringResources.TryGetValue(resourceName, out text))
{
Stream stream = null;
try
{
stream = assembly.GetManifestResourceStream(resourceName);
using (StreamReader reader = new StreamReader(stream, Encoding.Default))
{
text = reader.ReadToEnd();
text = text.Replace("\r\n", "\n");
stringResources.Add(resourceName, text);
}
}
catch (Exception e)
{
string exceptionMessage = string.Format("{0} assembly'si içinden {1} kaynağı yüklenemedi.", assembly.GetName().Name, resourceName);
throw new Exception(exceptionMessage, e);
}
finally
{
if (stream != null)
{
stream.Close();
}
}
}
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment