In my program, I use CSharpCodeProvider in order to compile another application from the source file, I use the code below:
public static bool CompileExecutable(String sourceName)
{
FileInfo sourceFile = new FileInfo(sourceName);
CodeDomProvider provider = null;
bool compileOk = false;
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)
{
String exeName = String.Format(@"{0}\{1}.exe",
System.Environment.CurrentDirectory,
sourceFile.Name.Replace(".", "_"));
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = true;
cp.OutputAssembly = exeName;
cp.GenerateInMemory = false;
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");
CompilerResults cr = provider.CompileAssemblyFromFile(cp,
sourceName);
if (cr.Errors.Count > 0)
{
Console.WriteLine("Errors building {0} into {1}",
sourceName, cr.PathToAssembly);
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Console.WriteLine();
}
}
else
{
Console.WriteLine("Source {0} built into {1} successfully.", sourceName, cr.PathToAssembly);
}
if (cr.Errors.Count > 0)
compileOk = false;
else
compileOk = true;
}
return compileOk;
}
I put a file with the name (source.cs) that contains the source code of the program I want to compile, I put it in the same directory as my application, and call the function from my application
CompileExecutable("source.cs");
Then the source code is compiled and saved in the same directory as my application.
What I'm trying to do now is add a parameter that allows me to select a custom icon for the compiled source code, so the icon will be selected for the executable output file, but I don’t know how I can assign an executable output icon before compiling it.
, ?