如何分配的是从源文件编译的应用程序自定义图标? [英] How to assign a custom icon for an application which is compiled from source file?
问题描述
在我的计划,我现在用的是CSHARP codeProvider为了从一个源文件编译另一个应用程序,使用code我'是如下:
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;
}
我把一个指定的文件(source.cs)包含我想从我的应用程序编译,我把它放在同一目录下我的应用程序,并调用该函数的程序的源$ C $ C
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");
然后,将源$ C $ C编译并保存到同一目录我的应用程序。
Then the source code is compiled and saved to same directory as my application.
我试图现在要做的是添加这让我选择编译源$ C $ C自定义图标的选项,因此输出的可执行文件将有我选择的图标,但我不知道该怎么我可以在编译之前指定输出的可执行文件的图标。
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?
推荐答案
要建立在 Brian的回答,您可以设置通过指定<一个自定义图标href="http://msdn.microsoft.com/en-us/library/2aaxe43f%28v=vs.71%29.aspx"><$c$c>/win32icon$c$c>编译器选项:
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";
这篇关于如何分配的是从源文件编译的应用程序自定义图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!