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

标签 c# .net icons csharpcodeprovider

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

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

我现在想做的是添加一个选项,让我为编译后的源代码选择一个自定义图标,这样输出的可执行文件就会有我选择的图标,但我不知道如何分配编译前输出可执行文件的图标。

关于如何在编译之前为输出可执行文件设置自定义图标的任何帮助?

最佳答案

Brian's answer 为基础,您可以通过指定 /win32icon 来设置自定义图标编译器选项:

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

关于c# - 如何为从源文件编译的应用程序分配自定义图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10125034/

相关文章:

c# - 表单 DataGridView 缺少 OnRowDataBound?

android - 删除白色背景并拥有全尺寸应用程序图标?

iphone - 苹果是否在每个图标上应用应用程序图标光泽效果和圆角?

C#访问内存映射文件

c# - 从数据表中删除特定行

c# - Elasticsearch Nest 带空格的通配符查询

C#时间管理问题

c# - 如何将 GUID 的一部分转换为长的?

asp.net - DropdownList 重置在更新面板的提交按钮上不起作用

android - air for android 图标尺寸太小