Domanda

In my program, I am using the CSharpCodeProvider in order to compile another application from a source file, the code i'am using is the below :

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

I put a file named (source.cs) which contain 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 to same directory as my application.

What I'm trying to do now is to add an option which let me select a custom icon for the compiled source code, so the output executable will have the icon i chose, but i don't know how can i assign the output executable's icon before compiling it.

Any help on how can i set a custom icon for the output executable before compiling it?

È stato utile?

Soluzione

To build on Brian's answer, you can set the custom icon by specifying the /win32icon compiler option:

CompilerParameters parameters = new CompilerParameters();
parameters.CompilerOptions = @"/win32icon:C:\full\path\to\icon.ico";

Altri suggerimenti

Check out the /win32icon compiler option.

Summary:

The /win32icon option inserts an .ico file in the output file, which gives the output file the desired appearance in the Windows Explorer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top