Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] How to Run Exe file as an Embedded Resource in C#

Monday, 14 December 2015
|
Read: Completed in minutes

[Tips] How to Run Exe file as an Embedded Resource in C#

Sometime you want to embedded your executable or dll file to another exe and run it with some conditions in projects. This topic is for you, follow my instruction, you can easy do it

First add the embedded executable file as resource file to your existing resource file,

If you don't have one, then you need to add existing item to your project, and select resource file

When you add the executable file in resource editor page, select type as [Files], then find your embedded executable file and add it.

How to Run Exe file as an Embedded Resource in C#


How to Run Exe file as an Embedded Resource in C#

How to Run Exe file as an Embedded Resource in C#



How to Run Exe file as an Embedded Resource in C#


When you add this Resource into your projects, you'll see Resource.cs and Resource.Designer.cs same as below image






How to Run Exe file as an Embedded Resource in C# 


For example my file named as "Engine.exe", then the resource design cs file will have following code added:



In Resources.Designer.cs file

//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.34209
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// 
//------------------------------------------------------------------------------

namespace LeanTechTips_BlogSpot_com.Properties {
    using System;
        
    /// 
    ///   A strongly-typed resource class, for looking up localized strings, etc.
    /// 
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }
        
        /// 
        ///   Returns the cached ResourceManager instance used by this class.
        /// 
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = 
                       new global::System.Resources.ResourceManager("LeanTechTips_BlogSpot_com.Properties.Resources", 
                                                                              typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// 
        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        /// 
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// 
        ///   Looks up a localized resource of type System.Byte[].
        /// 
        internal static byte[] Engine{
            get {
                object obj = ResourceManager.GetObject("Engine", resourceCulture);
                return ((byte[])(obj));
            }
        }

        public static byte[] GetEngine()
        {
            return Engine;
        }
    }
}


Add a method to access to your resource, which is also very simple, just add following code to your resource designer cs file

public static byte[] GetEngine()
{
    return Engine;
}

In your main executable source code, add following to read resource and write it to a new file

Get Current Directory by use:

Directory.GetCurrentDirectory()

Access to resource by use:

TanVoLam_Launcher.Properties.Resources.GetAutoDownload()
 
On the FrmMain.cs file. We go add new code extractAndRun to the program same as below code

public static void extractAndRun(string szName)
{
  try
  {
     string exeFileName = Path.Combine(Directory.GetCurrentDirectory(), szName);
     File.Delete(exeFileName);

     using (FileStream fsDst = new FileStream(exeFileName, 
                                FileMode.CreateNew, FileAccess.Write))
     {
        byte[] bytes = TanVoLam_Launcher.Properties.Resources.GetAutoDownload();
        fsDst.Write(bytes, 0, bytes.Length);
     }

     Process.Start(exeFileName);
  }
  catch { }
}

Use process to run the new executable file


Are you interested in topic How to Run Exe file as an Embedded Resource in C# from Webzone Tech Tips? If you have any thoughts or questions, please share them in the comment section below. I would love to hear from you and chat about it

Webzone Tech Tips Zidane


🙇🏼🙇🏼 Your Feedback Is Valuable and Helpful to Us - Webzone, all things Tech Tips web development Zidane
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com