如何为从源文件编译的应用程序分配自定义图标?

3

在我的程序中,我正在使用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");

然后源代码被编译并保存到与我的应用程序相同的目录中。

现在我正在尝试添加一个选项,让我选择一个自定义图标作为编译后源代码的图标,这样输出的可执行文件就有我选择的图标了,但我不知道如何在编译之前为输出的可执行文件指定图标。

请问如何在编译之前为输出的可执行文件设置自定义图标?


如果这些第三方工具可以被脚本化,您是否愿意考虑使用它们? - Joshua Drake
你说的第三方工具是什么意思?我必须使用其他工具来实现这个吗?有没有简单的方法为输出的可执行文件分配一个图标呢? - Rafik Bari
可能是VB 2008-更改生成的exe图标的重复问题。 - M.Babcock
2个回答

7

Brian 的回答 的基础上,你可以通过指定 /win32icon 编译选项来设置自定义图标:

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

3

请查看 /win32icon 编译选项。

概要:

/win32icon 选项会将一个 .ico 文件插入到输出文件中,从而使得输出文件在 Windows Explorer 中呈现出想要的外观。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接