0

Here is my codes :

  Assembly_Create(Application.StartupPath + @"\Out.exe");

  private static void Assembly_Create(string fileDestination)
   {
        CompilerParameters settings = new CompilerParameters(); //the heart of CodeDOM
        string compilerOptions = "/t:winexe /optimize";

        string icon_name = "my_icon.ico";
        string icon_location = Path.Combine(Application.StartupPath.Replace("\\bin\\Release", string.Empty), "images", icon_name);

        compilerOptions += " /win32icon:\"" + icon_location + "\"";

        settings.CompilerOptions = compilerOptions;
        settings.ReferencedAssemblies.AddRange(new string[] { "Ionic.Zip.dll", "Microsoft.CSharp.dll", ... });
        settings.GenerateExecutable = true;
        settings.WarningLevel = 4; 
        settings.OutputAssembly = fileDestination; 

        //settings.EmbeddedResources.Add();

        string sourceCode = Properties.Resources.Source;
        string assemblyInfo = Properties.Resources.AssemblyInfo;

        CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(settings, sourceCode, assemblyInfo);

        if (results.Errors.HasErrors)
        {

        }
        else
        {

        }
   }


And here is Source.txt in Resources Area :
I've created it by myself and put it in Resources Area.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ionic.Zip;
using System.Reflection;
using System.Resources;

namespace Game_Setup
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 frm = new Form2();
            frm.Show();
        }
    }

    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(98, 211);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }

    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(88, 174);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "salam";
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::Game_Setup.Properties.Resources.Logo;
            this.pictureBox1.Location = new System.Drawing.Point(0, 105);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(250, 143);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.PictureBox pictureBox1;
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


In Source.txt i put Form1.cs & Form1.Designer.cs & Form2.cs & Form2.Designer.cs & Program.cs Files.
Mean i gathered all of them in one file called Source.txt.

If i remove pictureBox1 from Form2 everything is ok and i have no error during compilation and biulding assembly.
But if i keep pictureBox1 i have error because of this line :
 this.pictureBox1.Image = global::Game_Setup.Properties.Resources.Logo;


And error is :

The type or namespace name 'Properties' does not exist in the namespace 'Game_Setup' (are you missing an assembly refrerence?)


How can i fix this error?
I think i should use this line :

//settings.EmbeddedResources.Add();   

But how can i embed Logo image into the new Assembly and how can i introduce it as pictureBox1 image?


I am using visual studio 2012 and here is the picture of Solution Explorer :

Solution Explorer Picture

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Do not put the code inside auto generated CS file. Init your picture box from other Initialize(..), for example, method. – Tigran Nov 20 '14 at 14:12
  • it *looks* like you are *writing* the code inside designer.cs file, which is a file generated by visual studio. if so, remove your custom code from that file and assign desired value to the `PictureBox` in some other place. – Tigran Nov 20 '14 at 14:23
  • In Source.txt i put Form1.cs & Form1.Designer.cs & Form2.cs & Form2.Designer.cs & Program.cs -> i combined all of these files in Source.txt because output assembly needs them and this is not my problem. i can give absolute path to pictureBox1 but i don't want extra files (Logo Image) next Out.exe file. – SilverLight Nov 20 '14 at 14:27
  • MoonLight: What is a Source.txt ?? – Tigran Nov 20 '14 at 14:28
  • here is a related thread : http://stackoverflow.com/questions/10234203/how-can-we-add-embedded-resources-to-a-file-which-is-compiled-from-a-source-file – SilverLight Nov 20 '14 at 14:50

2 Answers2

0

You are compiling Form2 by using CodeDom. And you want to display picture, which is resource of application, which performs compiling.

Obviously the easiest is to pass that image as parameter.

To example, it can be a static property of Form2:

public partial class Form2 : Form
{
    public static Image Image {get; set;}

    public Form2()
    {
        InitializeComponent();
        pictureBox1.Image = Image;
    }
}

It should compile without problem. When you going to use that assembly, simply set Image to your resource (not sure, so pseudo-code)

dynamic obj = results.CompiledAssembly;
obj.Form2.Image = global::Game_Setup.Properties.Resources.Logo;
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • thanks for the answer, i should tell you two things : 1-'System.Reflection.Assembly' does not contain a definition for 'Form2' error | 2-i want to use builded assembly in another system, not in my codes. so filling that property is useless. – SilverLight Nov 20 '14 at 16:01
0

I found this solution :
i added these codes to Assembly_Create() method :

    IResourceWriter writer = new ResourceWriter("myResources.resources");
    writer.AddResource("Logo", new Bitmap(Properties.Resources.Logo));
    writer.Generate();
    writer.Close();

    settings.EmbeddedResources.Add("myResources.resources");

and changed Source.txt file like this :

    ResourceManager RM = new ResourceManager("myResources", Assembly.GetExecutingAssembly());
    this.pictureBox1.Image = (Image)RM.GetObject("Logo");

Now every thing is ok and i can use my Out.exe file.
please complete my answer and show us other ways.

SilverLight
  • 19,668
  • 65
  • 192
  • 300