Wednesday, July 11, 2012

Класс для получения строки из Embeded Resources

Класс позволяющий получать строку из Embeded Resources. Удобно доставать sql из файлов в проекте.
    public class EmbeddedResource
    {
        private EmbeddedResource()
        {
        }

        public static StreamReader GetStream(System.Reflection.Assembly assembly, string name)
        {
            foreach (string resName in assembly.GetManifestResourceNames())
            {
                if (resName.EndsWith(name))
                {
                    return new System.IO.StreamReader(assembly.GetManifestResourceStream(resName));
                }
            }
            return null;
        }

        public static string GetString(System.Reflection.Assembly assembly, string name)
        {
            System.IO.StreamReader sr = EmbeddedResource.GetStream(assembly, name);
            string data = sr.ReadToEnd();
            sr.Close();
            return data;
        }

        public static string GetString(string name)
        {
            return EmbeddedResource.GetString(typeof (EmbeddedResource).Assembly, name);
        }
    }

No comments:

Post a Comment