How to create a standalone executable for each form for a C # solution in VS 2008 with multiple projects with multiple forms in the same assembly?

I have a C # based solution in Visual Studio 2008 for this problem domain. This solution has 1 + 5 projects. The first project is for resources that span the other 5 projects.

Each project has several forms. By default, the assembly generates one executable for each project. What I want, rather, is each form, which is not mutually dependent on the others, to create a standalone executable file.

I know that I could break each of the 5 main projects into several projects and set the “Startup Project” to “Several startup projects” and achieve my goal. But this will lead to overfilling of the solution with an increase in the number of projects to double digits. I do not want to go along the path of creating a separate solution for each of the 5 projects.

Now I call Application.Run as follows to accomplish my goal:

Application.Run(new fooForm());

and when I need to create an executable file for another independent form, I change the value of the form to

Application.Run(new barForm());

and so on, until I need another shot of espresso.

Anyone hack what I intend without me to jump out of a window?

+3
source share
5 answers

, EXE , Form. , EXE , EXE

  Assembly.GetExecutingAssembly().Location

, , "Application.Run".

EXE . , .

: ():

[STAThread]
static void Main(string[] args)
{
    if(args.Length<1)
         return;
    if(args[0]=="Form1")
         Application.Run(new Form1());
    else if(args[0]=="Form2")
         Application.Run(new Form2());
}
+2
  • .
  • /exe (csc.exe #), , () .

    public partial class Form2 : Form
    {
      [STAThread]
      static void Main()
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new Form2());
      }
    }
    

2, - :

csc.exe /out:Form2.exe Form2.cs Form2.designer.cs  /Main:WindowsFormsApplication1.Form2
+2

Doc Brown exe , , , exe (MyNamespace.MyForm.exe MyNamespace.MyForm as -)

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        string FullPath = Assembly.GetExecutingAssembly().Location;
        string ExeName = Path.GetFileNameWithoutExtension(FullPath);
        Type TypeBasedOnExeName = Assembly.GetExecutingAssembly().GetType(ExeName);
        Form StartupForm = (Form)Activator.CreateInstance(TypeBasedOnExeName);

        Application.Run(StartupForm);
    }
+1

Visual Studio , , .

, , ?

, ilasm.exe ildasm.exe -: 5 EXE > 5 EXE , .

0

I would suggest creating a template file .csprojand creating many of them with a small C # or Perl program, filling the template with values ​​based on the forms of your solution. You can then merge the files .csprojusing a batch or Perl script to compile using devenv.exefrom the command line.

There is no real "clean" way.

0
source

Source: https://habr.com/ru/post/1724689/


All Articles