Created
June 12, 2014 12:24
-
-
Save yemrekeskin/7589f6f3995740e16c05 to your computer and use it in GitHub Desktop.
Resource file manager in assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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