In this article, you will learn how to convert an MP3 file to an executable file.
- How to compile C# code during runtime
- How to play an MP3 file using C#
- How to insert a file into your application as an embedded resource and extract embedded resource dynamically during runtime
- How to implement dragging files from Explorer
- How to insert a file into your application as an embedded resource through designer.
Here is a description how this program works.
First the user chooses the MP3 files he wished to convert to EXE. After that, an instance of
CompilerParameters class is created and the specified MP3 file is added as an embedded resource through the
EmbeddedResources property of
CompilerParameters class. This is done in a worker thread using
BackGroundWorker component. The icon of the executable file can be chosen by the user and command line option is used to specify it.
The source code we compile is source code of a simple application that doesn't have a window, extracts the embedded MP3 file and plays that file. The source file that is compiled is itself embedded in the first application and extracted to temp folder at startup.
= new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
if (File.Exists(Application.StartupPath + "\\icon.ico"))
pathtoicon= Application.StartupPath + "\\icon.ico";
pathtoicon = this.pictureBox1.ImageLocation;
cp.CompilerOptions = "/target:winexe" + " " + "/win32icon:" + "\"" +
pathtoicon + "\"";
cp.GenerateExecutable = true;
cp.IncludeDebugInformation = false;
cp.OutputAssembly = sv.FileName;
cp.GenerateInMemory = false;
cp.TreatWarningsAsErrors = false;
string temp = Environment.GetEnvironmentVariable("TEMP");
CompilerResults cr = pr.CompileAssemblyFromFile(cp, temp + "\\it.cs");
MessageBox.Show("There was an error while converting the file","Error",
This portion of code is from the source file that is compiled by the program. This code extracts embedded resource from the application during runtime.
BinaryReader br = new BinaryReader(theResource);
FileStream fs = new FileStream(Environment.GetEnvironmentVariable("TEMP") +
+"\\it.mp3" , FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
byte bt = new byte[theResource.Length];
GetManifestResourceStream because there is only one resource.
To implement drag and drop functionality from Windows Explorer, I use the class that came with the source code of this book.
You need to create an instance of the
DragAndDropFileComponent class and set up event handler. Here is the code snippet:
DragAndDropFileComponent drag = new DragAndDropFileComponent(this.components);
drag.FileDropped += new FileDroppedEventHandler(drag_FileDropped);
drag.HostingForm = this;
Here is the event handler:
void drag_FileDropped(object sender, FileDroppedEventArgs e)
if (e.Filenames!=null &
e.Filenames.Length!=0 & e.Filenames.EndsWith(".mp3"))
this.textBox1.Text = e.Filenames;
In order to play an MP3 file from my application, I used this class. After you add
MP3Player to your project, playing MP3 files is very simple. Here is the code snippet from the source file that is compiled:
MP3Player pl = new MP3Player();
pl.Open(Environment.GetEnvironmentVariable("TEMP") + "\\it.mp3");
catch (Exception ex)
The program starts playing the MP3 file when it is loaded and quits when playing is over.
The idea itself of converting an MP3 file to an EXE file is a little bit strange and the whole application is interesting.
- 18th May, 2007 - Initial version
- 26th May, 2007 - Fixed the bug that was causing 100% CPU load when launching the generated EXE