C# 如何为从源文件编译的应用程序分配自定义图标?
在我的程序中,我使用CSharpCodeProvider从源文件编译另一个应用程序,我使用的代码如下:C# 如何为从源文件编译的应用程序分配自定义图标?,c#,.net,icons,csharpcodeprovider,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
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;
}
然后编译源代码并将其保存到与我的应用程序相同的目录中
我现在要做的是添加一个选项,让我为编译后的源代码选择一个自定义图标,这样输出可执行文件将具有我选择的图标,但我不知道如何在编译前分配输出可执行文件的图标
有关如何在编译输出可执行文件之前为其设置自定义图标的任何帮助信息?请查看编译器选项
总结:
/win32icon选项在输出文件中插入一个.ico文件,使输出文件在Windows资源管理器中具有所需的外观
要在上构建,可以通过指定编译器选项来设置自定义图标:
CompilerParameters parameters = new CompilerParameters();
parameters.CompilerOptions = @"/win32icon:C:\full\path\to\icon.ico";
你是否愿意考虑第三方工具,如果它们可以被脚本化?你指的是第三方工具,我必须使用其他工具来实现这一点吗?难道不能简单地为输出可执行文件分配一个图标吗
CompilerParameters parameters = new CompilerParameters();
parameters.CompilerOptions = @"/win32icon:C:\full\path\to\icon.ico";