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#
When you add this Resource into your projects, you'll see Resource.cs and Resource.Designer.cs same as below image
For example my file named as "Engine.exe", then the resource design cs file will have following code added:
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook://------------------------------------------------------------------------------ //
// 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[] Get
Engine
() { 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