在我的计划,我现在用的是CSharpCodeProvider为了编译从源文件的另一个应用程序,使用的代码我'是如下:
public static bool CompileExecutable(String sourceName)
{
FileInfo sourceFile = new FileInfo(sourceName);
CodeDomProvider provider = null;
bool compileOk = false;
// Select the code provider based on the input file extension.
if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
provider = CodeDomProvider.CreateProvider("CSharp");
else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
provider = CodeDomProvider.CreateProvider("VisualBasic");
else
{
Console.WriteLine("Source file must have a .cs or .vb extension");
}
if (provider != null)
{
// Format the executable file name.
// Build the output assembly path using the current directory
// and <source>_cs.exe or <source>_vb.exe.
String exeName = String.Format(@"{0}\{1}.exe",
System.Environment.CurrentDirectory,
sourceFile.Name.Replace(".", "_"));
CompilerParameters cp = new CompilerParameters();
// Generate an executable instead of a class library.
cp.GenerateExecutable = true;
// Specify the assembly file name to generate.
cp.OutputAssembly = exeName;
// Save the assembly as a physical file.
cp.GenerateInMemory = false;
// Set whether to treat all warnings as errors.
cp.TreatWarningsAsErrors = false;
cp.ReferencedAssemblies.Add("System.dll");
cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");
cp.ReferencedAssemblies.Add("mscorlib.dll");
cp.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll");
// Invoke compilation of the source file.
CompilerResults cr = provider.CompileAssemblyFromFile(cp,
sourceName);
if (cr.Errors.Count > 0)
{
// Display compilation errors.
Console.WriteLine("Errors building {0} into {1}",
sourceName, cr.PathToAssembly);
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Console.WriteLine();
}
}
else
{
// Display a successful compilation message.
Console.WriteLine("Source {0} built into {1} successfully.", sourceName, cr.PathToAssembly);
}
// Return the results of the compilation.
if (cr.Errors.Count > 0)
compileOk = false;
else
compileOk = true;
}
return compileOk;
}
我把一个文件名为(source.cs)包含我想编译程序的源代码,我把它放在同一个目录中我的应用程序,并从我的应用程序中调用该函数
CompileExecutable("source.cs");
然后将源代码被编译并保存到同一个目录中我的应用程序。
我试图现在要做的是添加这让我选择编译源代码自定义图标的选项,所以输出可执行文件将有我选择的图标,但我不知道我怎样才能分配输出可执行文件的在编译之前图标。
我如何能在编译之前设置输出可执行的自定义图标任何帮助吗?