Click here to Skip to main content
11,412,833 members (75,861 online)
Click here to Skip to main content

Convert MP3 to EXE

, 18 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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
Follow on   Google+

Comments and Discussions

Questionmp3 to exe Pin
Kalpana Volety at 30-Apr-14 9:01
memberKalpana Volety30-Apr-14 9:01 
QuestionAdd form for controlling mp3 Pin
cronikslacker at 15-Sep-10 4:49
membercronikslacker15-Sep-10 4:49 
GeneralTo .exe Pin
subewa at 20-May-10 23:12
membersubewa20-May-10 23:12 
RantMp3 Pin
Royce Ng Rui Jie at 5-Jan-10 23:26
memberRoyce Ng Rui Jie5-Jan-10 23:26 
GeneralGood job Pin
chaiguy1337 at 18-Jan-09 8:00
memberchaiguy133718-Jan-09 8:00 
GeneralRe: Good job Pin
Giorgi Dalakishvili at 18-Jan-09 8:02
mvpGiorgi Dalakishvili18-Jan-09 8:02 
GeneralBug Pin
Stuart Halliday at 2-Jan-09 4:05
memberStuart Halliday2-Jan-09 4:05 
GeneralSmall error Pin
Frits88 at 28-Sep-08 15:30
memberFrits8828-Sep-08 15:30 
GeneralRe: Small error Pin
Giorgi Dalakishvili at 28-Sep-08 21:19
mvpGiorgi Dalakishvili28-Sep-08 21:19 
GeneralRe: Small error Pin
Frits88 at 29-Sep-08 12:22
memberFrits8829-Sep-08 12:22 
NewsError downloading source code Pin
Steve Povah at 4-May-08 4:23
memberSteve Povah4-May-08 4:23 
GeneralRe: Error downloading source code Pin
Giorgi Dalakishvili at 4-May-08 5:24
mvpGiorgi Dalakishvili4-May-08 5:24 
Everything seems to be fine for me. I can download code.

Giorgi Dalakishvili

#region signature
my articles

GeneralA very interesting idea! Pin
TonyTonyQ at 24-Mar-08 18:49
memberTonyTonyQ24-Mar-08 18:49 
GeneralRe: A very interesting idea! Pin
Giorgi Dalakishvili at 24-Mar-08 23:20
mvpGiorgi Dalakishvili24-Mar-08 23:20 
General[Message Deleted] Pin
crysler at 20-Feb-08 13:16
member crysler 20-Feb-08 13:16 
GeneralRe: you read good books Pin
Giorgi Dalakishvili at 20-Feb-08 21:26
mvpGiorgi Dalakishvili20-Feb-08 21:26 
Question"ქართველი ხარ, ბიჭო"? Pin
LOTEBI at 11-Feb-08 17:16
memberLOTEBI11-Feb-08 17:16 
Questionneed to edit mp3 file Pin
Member 941697 at 24-Jan-08 1:59
memberMember 94169724-Jan-08 1:59 
QuestionWhat is the purpose of mp3 to exe converter? Pin
Member 4564787 at 7-Jan-08 23:10
memberMember 45647877-Jan-08 23:10 
AnswerRe: What is the purpose of mp3 to exe converter? Pin
Giorgi Dalakishvili at 8-Jan-08 0:57
mvpGiorgi Dalakishvili8-Jan-08 0:57 
GeneralRe: What is the purpose of mp3 to exe converter? Pin
Marty R at 15-Feb-09 5:15
memberMarty R15-Feb-09 5:15 
GeneralHistory Pin
Armando Airo' at 14-Nov-07 3:52
memberArmando Airo'14-Nov-07 3:52 
GeneralRe: History Pin
Jim Weiler at 15-Nov-07 12:49
memberJim Weiler15-Nov-07 12:49 
GeneralThreadStateException Pin
Greg Cadmes at 21-Aug-07 13:10
memberGreg Cadmes21-Aug-07 13:10 
GeneralRe: ThreadStateException Pin
Giorgi Dalakishvili at 22-Aug-07 0:18
memberGiorgi Dalakishvili22-Aug-07 0:18 
QuestionWhy? Pin
Michael Sync at 13-Aug-07 23:57
memberMichael Sync13-Aug-07 23:57 
AnswerRe: Why? Pin
Giorgi Dalakishvili at 13-Aug-07 23:59
memberGiorgi Dalakishvili13-Aug-07 23:59 
GeneralRe: Why? Pin
Michael Sync at 15-Aug-07 6:24
memberMichael Sync15-Aug-07 6:24 
GeneralRe: Why? Pin
Giorgi Dalakishvili at 15-Aug-07 6:39
memberGiorgi Dalakishvili15-Aug-07 6:39 
GeneralRe: Why? Pin
Michael Sync at 15-Aug-07 17:58
memberMichael Sync15-Aug-07 17:58 
Generalgood article, Pin
Juraj Borza at 6-Aug-07 21:40
memberJuraj Borza6-Aug-07 21:40 
GeneralRe: good article, Pin
Giorgi Dalakishvili at 7-Aug-07 1:00
memberGiorgi Dalakishvili7-Aug-07 1:00 
Generalnice concept! Pin
ThatsAlok at 5-Aug-07 20:46
memberThatsAlok5-Aug-07 20:46 
GeneralRe: nice concept! Pin
Giorgi Dalakishvili at 6-Aug-07 3:12
memberGiorgi Dalakishvili6-Aug-07 3:12 
GeneralSmall fix Pin at 23-May-07 9:31
memberibobalca@yahoo.com23-May-07 9:31 
GeneralRe: Small fix Pin
Giorgi Dalakishvili at 23-May-07 11:51
memberGiorgi Dalakishvili23-May-07 11:51 
GeneralRe: Small fix Pin
iulian.bobalca at 23-May-07 11:59
memberiulian.bobalca23-May-07 11:59 
GeneralRe: Small fix Pin
Giorgi Dalakishvili at 24-May-07 9:45
memberGiorgi Dalakishvili24-May-07 9:45 
GeneralRe: Small fix Pin
Karsten Schulz at 20-Jun-07 5:54
memberKarsten Schulz20-Jun-07 5:54 
GeneralExcellent Pin
merlin981 at 22-May-07 4:42
membermerlin98122-May-07 4:42 
GeneralNice work Pin
Jim Tebbutt at 22-May-07 2:29
memberJim Tebbutt22-May-07 2:29 
GeneralExcellent.!!!!! Pin
Niiiissssshhhhhuuuuu at 20-May-07 19:42
memberNiiiissssshhhhhuuuuu20-May-07 19:42 
GeneralCPU Utilization 100% Pin
_Ratish_Philip_ at 20-May-07 17:51
member_Ratish_Philip_20-May-07 17:51 
GeneralRe: CPU Utilization 100% Pin
Giorgi Dalakishvili at 21-May-07 2:56
memberGiorgi Dalakishvili21-May-07 2:56 
GeneralRe: CPU Utilization 100% Pin
Frank Ouimette at 21-May-07 19:25
memberFrank Ouimette21-May-07 19:25 
GeneralRe: CPU Utilization 100% Pin
Giorgi Dalakishvili at 21-May-07 22:05
memberGiorgi Dalakishvili21-May-07 22:05 
GeneralRe: CPU Utilization 100% Pin
Giorgi Dalakishvili at 26-May-07 9:12
memberGiorgi Dalakishvili26-May-07 9:12 
Generalsimple question ... Pin
BillWoodruff at 20-May-07 15:25
memberBillWoodruff20-May-07 15:25 
GeneralRe: simple question ... Pin
Giorgi Dalakishvili at 21-May-07 2:54
memberGiorgi Dalakishvili21-May-07 2:54 
GeneralRe: simple question ... Pin
u9900517 at 21-May-07 21:21
memberu990051721-May-07 21:21 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.1 | Last Updated 18 Mar 2008
Article Copyright 2007 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid