将 MP3 转换为 EXE






4.04/5 (89投票s)
一篇关于如何将 MP3 文件转换为可执行文件的文章。

目录
引言
在本文中,你将学习如何将 MP3 文件转换为可执行文件。
你将从本文中学到什么
- 如何在运行时编译 C# 代码
- 如何使用 C# 播放 MP3 文件
- 如何将文件作为嵌入资源插入到应用程序中,并在运行时动态提取嵌入资源
- 如何实现从资源管理器拖动文件
- 如何通过设计器将文件作为嵌入资源插入到应用程序中。
转换
以下是此程序工作原理的描述。
首先,用户选择他希望转换为 EXE 的 MP3 文件。 之后,创建 CompilerParameters
类的实例,并将指定的 MP3 文件通过 CompilerParameters
类的 EmbeddedResources
属性添加为嵌入资源。 这是在使用 BackGroundWorker
组件的工作线程中完成的。 可执行文件的图标可以由用户选择,并使用命令行选项指定。
我们编译的源代码是一个简单的应用程序的源代码,该应用程序没有窗口,提取嵌入的 MP3 文件并播放该文件。 被编译的源文件本身嵌入在第一个应用程序中,并在启动时提取到 *temp* 文件夹。
实现细节
动态编译源代码文件
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
}
在运行时提取嵌入的文件
这部分代码来自程序编译的源文件。 此代码在运行时从应用程序中提取嵌入的资源。
//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();
我们将 myassambly[0]
传递给 GetManifestResourceStream
,因为只有一个资源。
拖放
为了实现从 Windows 资源管理器拖放功能,我使用了 这本书的源代码附带的类。
你需要创建 DragAndDropFileComponent
类的实例并设置事件处理程序。 这是代码片段
DragAndDropFileComponent drag = new DragAndDropFileComponent(this.components);
drag.BeginInit();
drag.FileDropped += new FileDroppedEventHandler(drag_FileDropped);
drag.HostingForm = this;
drag.EndInit();
这是事件处理程序
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];
}
}
播放 MP3 文件
为了从我的应用程序播放 MP3 文件,我使用了这个类。 将 MP3Player
添加到你的项目后,播放 MP3 文件非常简单。 这是来自被编译的源文件的代码片段
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)
{ }
程序在加载时开始播放 MP3 文件,并在播放结束后退出。
最终想法
将 MP3 文件转换为 EXE 文件的想法本身有点奇怪,整个应用程序都很有趣。
历史
- 2007 年 5 月 18 日 - 初始版本
- 2007 年 5 月 26 日 - 修复了导致启动生成的 EXE 时 CPU 占用率达到 100% 的错误