11,487,721 members (73,491 online)

# Convert MP3 to EXE

, 18 Mar 2008 CPOL 163.1K 4K 171
 Rate this:
An article showing how to convert MP3 file to executable file

## Introduction

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

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";
}

{
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.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

//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
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

## Share

Software Developer
Georgia
No Biography provided

 [Message Deleted] crysler 20-Feb-08 13:16 crysler 20-Feb-08 13:16
 Re: you read good books Giorgi Dalakishvili20-Feb-08 21:26 Giorgi Dalakishvili 20-Feb-08 21:26
 "ქართველი ხარ, ბიჭო"? LOTEBI11-Feb-08 17:16 LOTEBI 11-Feb-08 17:16
 need to edit mp3 file Member 94169724-Jan-08 1:59 Member 941697 24-Jan-08 1:59
 Hi, I need to parse voice and music from mp3 file. Can you help me for this one?? I will be very thankful to you....! Himanshu.
 What is the purpose of mp3 to exe converter? Member 45647877-Jan-08 23:10 Member 4564787 7-Jan-08 23:10
 Re: What is the purpose of mp3 to exe converter? Giorgi Dalakishvili8-Jan-08 0:57 Giorgi Dalakishvili 8-Jan-08 0:57
 Re: What is the purpose of mp3 to exe converter? Marty R15-Feb-09 5:15 Marty R 15-Feb-09 5:15
 History Armando Airo'14-Nov-07 3:52 Armando Airo' 14-Nov-07 3:52
 Re: History Jim Weiler15-Nov-07 12:49 Jim Weiler 15-Nov-07 12:49