Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that compiles C# programs, to add images in the resources of the executable / dll, one can use the "EmbeddedResources" property of the compiler which comes from Microsoft.CSharp.CSharpCodeProvider. But to add the icon to the program, you need a Win32 resource for example. I have the paths of the images that the user adds and the program checks if the images exist in case the image gets deleted or blah blah blah.

What I have tried:

I tried to take the image path string but it says "Error CS1583: blah blah blah", you know it. I forgot to mention that the images are *.ico files. To use the property, I used these lines of code:
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

                // Build the parameters for source compilation. 
                CompilerParameters vb = new CompilerParameters();

                // Add an assembly reference.
                vb.ReferencedAssemblies.Add("System.dll");
                vb.ReferencedAssemblies.Add(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\System.Drawing.dll");

                // Add embedded resource.
                for (int i = 0; i < ImagesPath.ToArray().Length; i++)
                {
                    if (File.Exists(ImagesPath[i]))
                    {
                        Image img = Image.FromFile(ImagesPath[i]);
                        if (img.Height <= 32 && img.Width <= 32)
                        {
                            vb.Win32Resource += ImagesPath[i];
                        }
                    }
                }
                // The rest of the code (not important). Note that the variable ImagesPath is a string list (List<string>).

So the code you see is to add a Win32 resource to the assembly that is going to be compiled. I tried blocking the maximum size at 32x32 pixels but it didn't help.
Posted
Updated 29-Jun-22 13:56pm

1 solution

That Win32Resource property is not for icon files but for one compiled resource file as would be created by the resource compiler, RC.exe. If you were using the csc.exe compiler directly you would be able to use the win32icon option to add an icon for display by explorer.

Unfortunately the CSharpCodeProvider and CompilerParameters classes do not provide a win32icon property. However the CompilerParameters class does have a CompilerOptions string property to add any compiler options as text just as you would on the command line.

The example I'm pasting is just lifted from some code I have and hopefully is self explanatory.

C#
private CompilerResults CompileCode(CodeDomProvider provider, String sourceFileName, String exeFileName) {
  // Configure a CompilerParameters that links System.dll and produces the specified executable file.
  String[] referenceAssemblies = { "System.dll" };
  CompilerParameters parameters = new CompilerParameters(referenceAssemblies, exeFileName, false);
  // Generate an executable rather than a DLL file.
  parameters.GenerateExecutable = true;
  // Add an icon /win32icon:filename
  parameters.CompilerOptions = @"/win32icon:""D:\VC\Projects\BCL\CodeDom\CodeDomTests\union flag.ico""";

  // Invoke compilation.
  CompilerResults cr = provider.CompileAssemblyFromFile(parameters, sourceFileName);
  // Return the results of compilation.
  return cr;
}

Alan.
 
Share this answer
 
Comments
Le lance Flamer 2022 30-Jun-22 5:18am    
OK, I guess it works but when I compile the code, an error that is simply "Non-conforming characters in the path. No more or no less information. I've tried every way (like taking an image without spaces in its path or name but nothing changes. I have searched but I can't find the error because on the forums and don't show where the error is when they say "it's ok, I fixed it!". Thanks for the code though.
Alan N 30-Jun-22 10:25am    
What is the string assigned to CompilerOptions that is causing the error?
Le lance Flamer 2022 30-Jun-22 11:22am    
The string is C:\Icons\Icon.ico (this is the true path)
I don't know if the icon is too large.
Le lance Flamer 2022 5-Jul-22 4:30am    
I am still searching where is the problem but still nothing... I tried one icon, nope. Still that exception of the non-conforming characters in the path. Thanks for any help for that error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900