Translate

Dienstag, 8. August 2017

C# Embedded resources for "installer" like behaviour-Example


http://stackoverflow.com/questions/859699/how-to-add-resources-in-separate-folders


Einfach die Ordner (Und Dateien --> Bilder, SQL-Skripte, ...) in Ordner einfügen, zum Projekt hinzufügen.

Dann bei allen auf "Embedded resource" stellen:




Für SQL:

public MainWindow()
        {
            InitializeComponent();
            string commandText;
            Assembly thisAssembly = Assembly.GetExecutingAssembly();
            using (Stream s = thisAssembly.GetManifestResourceStream("TestAppInstaller.Resource.Test.Test.sql"))
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    commandText = sr.ReadToEnd();
                }
            }
            Debug.Print($"SQL commando: {commandText}");
        }

Für Bilder:

using(Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("<root namespace for the assembly>.<folder name>.<image file name>"))
{
   pictureBox1.Image = Image.FromStream(stream);
}

Für Bilder:
private void SetRedImage()
        {
            this.UiThreadInvoke(() => { PictureBox_Online.Image = GetImage("SchichtbuchPlaner2.Images.Red.ico"); });
        }

        private Image GetImage(string path)
        {
            using (Stream stream = GetStream(path))
            {
                return new Icon(stream).ToBitmap();
            }
        }

        private Stream GetStream(string path)
        {
            return Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
        } 

Keine Kommentare:

Kommentar veröffentlichen