Click here to Skip to main content
Click here to Skip to main content
Go to top

Convert MP3 to EXE

, 18 Mar 2008
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

Contents

Introduction

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.

Conversion

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.ReferencedAssemblies.Add("System.Deployment.dll");
cp.ReferencedAssemblies.Add("System.Drawing.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("System.Xml.dll");
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
br.Close();
bw.Close();

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.BeginInit();
drag.FileDropped += new FileDroppedEventHandler(drag_FileDropped);
drag.HostingForm = this;
drag.EndInit();

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();
try
   {
    pl.Open(Environment.GetEnvironmentVariable("TEMP") + "\\it.mp3");
    pl.Play();
    //wait until the file is played and then quit
    //this no longer causes 100% CPU utilization
    System.Threading.Thread.Sleep(((int)pl.AudioLength)+1);
    Application.Exit();
    }
    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.

History

  • 18th May, 2007 - Initial version
  • 26th May, 2007 - Fixed the bug that was causing 100% CPU load when launching the generated EXE

License

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

Share

About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided
Follow on   Google+

Comments and Discussions

 
Questionmp3 to exe PinmemberKalpana Volety30-Apr-14 8:01 
QuestionAdd form for controlling mp3 Pinmembercronikslacker15-Sep-10 3:49 
GeneralTo .exe Pinmembersubewa20-May-10 22:12 
RantMp3 PinmemberRoyce Ng Rui Jie5-Jan-10 22:26 
GeneralGood job Pinmemberchaiguy133718-Jan-09 7:00 
GeneralRe: Good job PinmvpGiorgi Dalakishvili18-Jan-09 7:02 
GeneralBug PinmemberStuart Halliday2-Jan-09 3:05 
GeneralSmall error PinmemberFrits8828-Sep-08 14:30 
GeneralRe: Small error PinmvpGiorgi Dalakishvili28-Sep-08 20:19 
GeneralRe: Small error PinmemberFrits8829-Sep-08 11:22 
NewsError downloading source code PinmemberSteve Povah4-May-08 3:23 
GeneralRe: Error downloading source code PinmvpGiorgi Dalakishvili4-May-08 4:24 
Everything seems to be fine for me. I can download code.
 
Giorgi Dalakishvili
 
#region signature
my articles
#endregion

GeneralA very interesting idea! PinmemberTonyTonyQ24-Mar-08 17:49 
GeneralRe: A very interesting idea! PinmvpGiorgi Dalakishvili24-Mar-08 22:20 
General[Message Deleted] Pinmember crysler 20-Feb-08 12:16 
GeneralRe: you read good books PinmvpGiorgi Dalakishvili20-Feb-08 20:26 
Question"ქართველი ხარ, ბიჭო"? PinmemberLOTEBI11-Feb-08 16:16 
Questionneed to edit mp3 file PinmemberMember 94169724-Jan-08 0:59 
QuestionWhat is the purpose of mp3 to exe converter? PinmemberMember 45647877-Jan-08 22:10 
AnswerRe: What is the purpose of mp3 to exe converter? PinmvpGiorgi Dalakishvili7-Jan-08 23:57 
GeneralRe: What is the purpose of mp3 to exe converter? PinmemberMarty R15-Feb-09 4:15 
GeneralHistory PinmemberArmando Airo'14-Nov-07 2:52 
GeneralRe: History PinmemberJim Weiler15-Nov-07 11:49 
GeneralThreadStateException PinmemberGreg Cadmes21-Aug-07 12:10 
GeneralRe: ThreadStateException PinmemberGiorgi Dalakishvili21-Aug-07 23:18 
QuestionWhy? PinmemberMichael Sync13-Aug-07 22:57 
AnswerRe: Why? PinmemberGiorgi Dalakishvili13-Aug-07 22:59 
GeneralRe: Why? PinmemberMichael Sync15-Aug-07 5:24 
GeneralRe: Why? PinmemberGiorgi Dalakishvili15-Aug-07 5:39 
GeneralRe: Why? PinmemberMichael Sync15-Aug-07 16:58 
Generalgood article, PinmemberJuraj Borza6-Aug-07 20:40 
GeneralRe: good article, PinmemberGiorgi Dalakishvili7-Aug-07 0:00 
Generalnice concept! PinmemberThatsAlok5-Aug-07 19:46 
GeneralRe: nice concept! PinmemberGiorgi Dalakishvili6-Aug-07 2:12 
GeneralSmall fix Pinmemberibobalca@yahoo.com23-May-07 8:31 
GeneralRe: Small fix PinmemberGiorgi Dalakishvili23-May-07 10:51 
GeneralRe: Small fix Pinmemberiulian.bobalca23-May-07 10:59 
GeneralRe: Small fix PinmemberGiorgi Dalakishvili24-May-07 8:45 
GeneralRe: Small fix PinmemberKarsten Schulz20-Jun-07 4:54 
GeneralExcellent Pinmembermerlin98122-May-07 3:42 
GeneralNice work PinmemberJim Tebbutt22-May-07 1:29 
GeneralExcellent.!!!!! PinmemberNiiiissssshhhhhuuuuu20-May-07 18:42 
GeneralCPU Utilization 100% Pinmember_Ratish_Philip_20-May-07 16:51 
GeneralRe: CPU Utilization 100% PinmemberGiorgi Dalakishvili21-May-07 1:56 
GeneralRe: CPU Utilization 100% PinmemberFrank Ouimette21-May-07 18:25 
GeneralRe: CPU Utilization 100% PinmemberGiorgi Dalakishvili21-May-07 21:05 
GeneralRe: CPU Utilization 100% PinmemberGiorgi Dalakishvili26-May-07 8:12 
Generalsimple question ... PinmemberBillWoodruff20-May-07 14:25 
GeneralRe: simple question ... PinmemberGiorgi Dalakishvili21-May-07 1:54 
GeneralRe: simple question ... Pinmemberu990051721-May-07 20: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 | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Mar 2008
Article Copyright 2007 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid