Click here to Skip to main content
14,608,247 members

Convert MP3 to EXE

Rate this:
4.04 (89 votes)
Please Sign up or sign in to vote.
4.04 (89 votes)
18 Mar 2008CPOL
An article showing how to convert MP3 file to executable file
Sample Image - maximum width is 600 pixels



In this article, you will learn how to convert an MP3 file to an executable file.

What You Will Learn From This Article

  1. How to compile C# code during runtime
  2. How to play an MP3 file using C#
  3. How to insert a file into your application as an embedded resource and extract embedded resource dynamically during runtime
  4. How to implement dragging files from Explorer
  5. 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.

Implementation Details

Compiling Source File Dynamically

Microsoft.CSharp.CSharpCodeProvider pr
                                 = new Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
string pathtoicon="";       // pathtoicon variable holds the path of the icon 
                           // for generated executable
if (File.Exists(Application.StartupPath + "\\icon.ico"))
    pathtoicon= Application.StartupPath + "\\icon.ico";

if (skinRadioButton2.Checked)
    pathtoicon = this.pictureBox1.ImageLocation;

cp.CompilerOptions = "/target:winexe" + " " + "/win32icon:" + "\"" + 
                         pathtoicon + "\"";    // specify options for compiler
cp.GenerateExecutable = true;                  // yes, generate an EXE file
cp.IncludeDebugInformation = false;            // here we add the mp3 file as 
                                               // as an embedded resource
cp.EmbeddedResources.Add(this.textBox1.Text);  // were to save the executable 
                                               // specified by savefiledialog
cp.OutputAssembly = sv.FileName;

cp.GenerateInMemory = false;
cp.ReferencedAssemblies.Add("System.dll");         // this and the following 
cp.ReferencedAssemblies.Add("System.Data.dll");    // lines add references
cp.TreatWarningsAsErrors = false;

string temp = Environment.GetEnvironmentVariable("TEMP");
// compile the source file
CompilerResults cr = pr.CompileAssemblyFromFile(cp,  temp + "\\it.cs"); 
if (cr.Errors.Count>0)
    MessageBox.Show("There was an error while converting the file","Error",
                 MessageBoxButtons.OK,MessageBoxIcon.Error);   //error checking

Extracting Embedded File During Runtime

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.

//this code requires System.Reflection namespace

//get names of resources in the assembly
string[] myassembly
     = Assembly.GetExecutingAssembly().GetManifestResourceNames();

//create stream from the resource. 
Stream theResource 
   = Assembly.GetExecutingAssembly().GetManifestResourceStream(myassembly[0]);
//Create binary reader from the stream

BinaryReader br = new BinaryReader(theResource);  
//then filestream
FileStream fs = new FileStream(Environment.GetEnvironmentVariable("TEMP") +
                                +"\\it.mp3" , FileMode.Create); 
BinaryWriter bw = new BinaryWriter(fs);    //and then binary writer
byte[] bt = new byte[theResource.Length];  //read the resource
theResource.Read(bt,0, bt.Length);         //and then write to the file
bw.Write(bt);                              //don't forget to close all streams

We pass myassambly[0] to GetManifestResourceStream because there is only one resource.

Drag and Drop

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[0].EndsWith(".mp3"))
  this.textBox1.Text = e.Filenames[0];

Playing MP3 File

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");
    //wait until the file is played and then quit
    //this no longer causes 100% CPU utilization
    catch (Exception ex)
    { }   

The program starts playing the MP3 file when it is loaded and quits when playing is over.

Final Thoughts

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided

Comments and Discussions

GeneralThreadStateException Pin
Greg Cadmes21-Aug-07 12:10
MemberGreg Cadmes21-Aug-07 12:10 
GeneralRe: ThreadStateException Pin
Giorgi Dalakishvili21-Aug-07 23:18
mentorGiorgi Dalakishvili21-Aug-07 23:18 
QuestionWhy? Pin
Michael Sync13-Aug-07 22:57
MemberMichael Sync13-Aug-07 22:57 
AnswerRe: Why? Pin
Giorgi Dalakishvili13-Aug-07 22:59
mentorGiorgi Dalakishvili13-Aug-07 22:59 
GeneralRe: Why? Pin
Michael Sync15-Aug-07 5:24
MemberMichael Sync15-Aug-07 5:24 
GeneralRe: Why? Pin
Giorgi Dalakishvili15-Aug-07 5:39
mentorGiorgi Dalakishvili15-Aug-07 5:39 
GeneralRe: Why? Pin
Michael Sync15-Aug-07 16:58
MemberMichael Sync15-Aug-07 16:58 
Generalgood article, Pin
Juraj Borza6-Aug-07 20:40
MemberJuraj Borza6-Aug-07 20:40 
although there is a space for improvements, for example you should not have static name for copied file you are about to convert to mp3.

What if some individual would want to mass-use this program to create many mp3-exe packages at once? Mp3 file should get an unique name so multiple instances of this applications won't overwrite their TEMP\it.mp3 files.

But the concept is good, really.
GeneralRe: good article, Pin
Giorgi Dalakishvili7-Aug-07 0:00
mentorGiorgi Dalakishvili7-Aug-07 0:00 
Generalnice concept! Pin
ThatsAlok5-Aug-07 19:46
MemberThatsAlok5-Aug-07 19:46 
GeneralRe: nice concept! Pin
Giorgi Dalakishvili6-Aug-07 2:12
mentorGiorgi Dalakishvili6-Aug-07 2:12 
GeneralSmall fix Pin
iulian.bobalca23-May-07 8:31
Memberiulian.bobalca23-May-07 8:31 
GeneralRe: Small fix Pin
Giorgi Dalakishvili23-May-07 10:51
mentorGiorgi Dalakishvili23-May-07 10:51 
GeneralRe: Small fix Pin
iulian.bobalca23-May-07 10:59
Memberiulian.bobalca23-May-07 10:59 
GeneralRe: Small fix Pin
Giorgi Dalakishvili24-May-07 8:45
mentorGiorgi Dalakishvili24-May-07 8:45 
GeneralRe: Small fix Pin
Karsten Schulz20-Jun-07 4:54
professionalKarsten Schulz20-Jun-07 4:54 
GeneralExcellent Pin
merlin98122-May-07 3:42
professionalmerlin98122-May-07 3:42 
GeneralNice work Pin
Jim Tebbutt22-May-07 1:29
MemberJim Tebbutt22-May-07 1:29 
GeneralExcellent.!!!!! Pin
Niiiissssshhhhhuuuuu20-May-07 18:42
MemberNiiiissssshhhhhuuuuu20-May-07 18:42 
GeneralCPU Utilization 100% Pin
Ratish Philip20-May-07 16:51
MemberRatish Philip20-May-07 16:51 
GeneralRe: CPU Utilization 100% Pin
Giorgi Dalakishvili21-May-07 1:56
mentorGiorgi Dalakishvili21-May-07 1:56 
GeneralRe: CPU Utilization 100% Pin
Frank Ouimette21-May-07 18:25
MemberFrank Ouimette21-May-07 18:25 
GeneralRe: CPU Utilization 100% Pin
Giorgi Dalakishvili21-May-07 21:05
mentorGiorgi Dalakishvili21-May-07 21:05 
GeneralRe: CPU Utilization 100% Pin
Giorgi Dalakishvili26-May-07 8:12
mentorGiorgi Dalakishvili26-May-07 8:12 
Generalsimple question ... Pin
BillWoodruff20-May-07 14:25
mveBillWoodruff20-May-07 14:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 19 May 2007


173 bookmarked