Generate Winforms Project .exe programmatically from within a Winforms Application - c#

I have a requirement to build a container C# WinForm Application which will spit out .exe files of another WinForm Application on a button click event.
For eg: I have one Winform App named ProjectA which accepts one startup argument. Now I have a container WinForm App named ProjectB. I want to generate ProjectA.exe programmatically within ProjectB by passing the required parameter to ProjectA app on the button click.
Could not find anything relevant about this on Google. Can anyone throw in some light to achieve this.
Please note that both the Winforms Application must be written in C#.
I have one very vague thought of using MSBuild Command to build ProjectA which in turn will generate its .exe
However I am not too clear on this.

It is not necessary to mess with MSBuild for this. You may generate your program's code as a string and then generate your executables on the fly:
https://support.microsoft.com/en-us/help/304655/how-to-programmatically-compile-code-using-c-compiler

As an example:
Process.Start(#"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe", #"d:\app1\app1.sln");
Another Example:
Process.Start(#"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe", #"d:\app1\app1.sln /t:Rebuild /p:Configuration=Debug;TargetFrameworkVersion=v4.0");
put the above code on your button click.

Related

Where can i run my code in visual studio?

So, i am trying to write my first C# "Hello World" Program in visual studio, however when i want to see the output of the code (or simply just run it), i can't because i can find the run option, i have tried ctrl + f5, and many other options, however it didn't worked any suggestions? Here's a picture of my visual studio.
Check this guide Tutorial: Create a simple C# console app in Visual Studio
Open Visual Studio 2019.
On the start window, choose Create a new project.
On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.
In the Configure your new project window, type or enter Calculator in the Project name box. Then, choose Create.
And then to run:
Choose the green Start button next to Calculator to build and run your program, or press F5.
This is because you created empty solution. You don't have any output, i.e. process of compilation of your solution does not produce any executable (exe) or library (dll).
In order to run any code is to produce one of the above. In order to do that you have to have project created.
To do so, you have to (in your case) right click solution and choose "Add -> New Project" option. This will show you pop up allowing you to choose which project you want to add, choose just basic type of project, which is "Console app". There you will have class with static method Main which is the entry point for your app.
Code inside Main method will be executed.
You haven't created a project. You've got no executable and no ability to build one. I don't know if you've worked with other languages before or not (perhaps ones which are interpreted and/or have no specific structure to them), but C#/.NET programs require a particular structure in order to build and execute. You can't just create a random C# file on its own and execute it.
Instead, create a new Console Application project (from the File -> New Project menu) and give it a name.
That will auto-generate a Program.cs file, which contains the Main method which is the entry point of your program when it's executed. From there you can write code in there, create other methods in that class if you need to, and you can also add other files containing classes etc. into your project.
(There are, as you'll see, also quite a lot of other project types you can create depending on the kind of application you want - and also some types which are just for code libraries and not full applications.)
How to: Run a C# program in Visual Studio
The project should contain a C# file with a Main method, and its output should be an executable (EXE), you have created an empty solution with no such files.

C# - generate DLL linked in console App

I have two C# Applications
TestApp is a console app which call a method decrypt included in AppCrypt
AppCrypt contains this method and needs to log (write on a file)
So far I can modify TestApp and I saw the result on my console but my goal is to edit the code of AppCrypt
I am using VS for both.
Question 1) I cannot find where is written the place where I link the DLL of AppCrypt
Question 2) I can build AppCrypt but I cannot generate a new DLL because I am getting this message
I found on SO that I should add a project Console App or a Winform but I just need to use that function without having a proper app so I dont know if it is my case.
I have built the DLL but it does not seems to be linked in my console app which is TestApp, how can I verify that it is linked?
You are getting that error cause most probably you have set your class library project as Startup project in your solution. Whereas you should set the Console app as your start up.
How to Set a project as StartUp: Right click on your project name and from context menu select Set As Startup Project

What runs my C# application?

When developing a C# project in Visual Studio, I have three options for output type. Console Application, Windows Application, and Class Library. AFAIK, the only difference between a DLL and an EXE, is the EXE should have an entry point defined, and that is called when the file is double clicked. However, when I launch an EXE built from a Console Application, a console window is created. So obviously something is happening other than the CLR getting launched and then calling my Main method. What launches the console window? When I launch an EXE built from a Windows Application, is some other code run also, or is it just my main method?
Your portable executable file(exe) will contain the information about what kind of application it is.
Subsystem flag of the IMAGE_OPTIONAL_HEADER defines what kind of user interface the application is built with.
IMAGE_SUBSYSTEM_WINDOWS_CUI defines the console application, IMAGE_SUBSYSTEM_WINDOWS_GUI defines the windows application and so on.
For more information Peering Inside the PE: A Tour of the Win32 Portable Executable File Format
The output type is a configuration parameter for your project which tells Visual Studio what to do when you compiled. If set to Console Application, it will generate an exe file with the code to launch the console window.
The different between a dll and an exe is more than the main method. Visual Studio generated additional codes in the exe file that creates the console and invoke the main method. For details of how the exe file performs this, refer to http://en.wikipedia.org/wiki/Portable_Executable.
In this link the inquisitor added some notes which mentioned the blog post (2nd link).
Is it possible to build a Console app that does not display a console Window when double-clicked?
http://blogs.msdn.com/b/oldnewthing/archive/2009/01/01/9259142.aspx
The same content as Siram's answer https://stackoverflow.com/a/30084790/2005526 but to assist you with future searches these were the keywords used on google to locate the mentioned resources. "double click exe launches console"

C# Add external user control to project

I'm working on a windows form application in C# VS2010.
I found on the internet an implementation of a User-Control that I want to use in my project.
Problem: how can I import or include it so I will be able to see it in my Toolbox ?
I copied the UserControl directory into my project directory(see attached photo)
and then build the project and got this warning:
Error 1 Unable to create a manifest resource name for "RecessEditorControl.resx". Could not find file 'C:\Users\bassam\Desktop\comm\Projects\Thunder Archive\Thunder Archive\RecessEditorControl.cs'. Thunder Archive
any idea how it should be done?
Open the user control project separately (outside of your solution) and attempt to build it.
If the build fails, search for help from the control author(s) on what dependencies are needed and how you can build the control.
After you get it sucesfully built, there will be a .dll file in the bin/ folder (where config is most likely debug or release, depending on what configuration you used to build the project)
Reference this .dll from the windows desktop solution you are working on. Add it to the references section of your UI project.
It should automatically show up in the toolbox if it is properly coded as a user control.
There is the option of including the entire source project of your control in your win app solution, you can try this later if you verify that you are able to build the control on it's own.
Although I would not recommend this approach unless you intent to actively continue developing the user control.
You separately build user control to create a dll. After that include the dll in your app executable folder. When you switch to designer, you will see the control appears on the toolbox with the control name. Just drag n drop...

Where to get Microsoft.practices.composite.unityextension.dll

Where to find Microsoft.Practices.Composite.UnityExtensions.dll for Microsoft.Practices.Unity.dll thats of version 2.1.505.2..
I was using 1.2.0.0 version of unity dll . While i changed the version to 2.1.505.2 the code gave me an error saying unityextension is depenedent to 1.2.0.0 version of unity dll ...
the version I am using is 2.0.0.0 of unityextension.
I really need help .. thanks in advance
Follow below steps as documented in official website :-
1.In Visual Studio, create a new Silverlight application. To do this, point to New on the File menu, and then click Project. In the Project
types list, click Silverlight in the Visual C# node. In the Templates
box, click Silverlight Application. Finally, set the project's name,
such as CALApplication1, specify a valid location, and then click OK.
This project will be the Shell project of your application.
2.On the Add Silverlight Application dialog box, make sure the Add a new ASP.NET Web project to the solution to host Silverlight option is
selected, and then click OK.
3.(Optional) In Windows Explorer, create a folder named Library inside your solution's folder, and then copy the following assemblies into
the folder
â—¦Microsoft.Practices.Composite.UnityExtensions.dll. This assembly
contains base and utility classes you can reuse in applications based
on the Composite Application Library for Silverlight that consume the
Unity Application Block. For example, it contains a bootstrapper base
class, the UnityBootstrapper class, that creates and configures a
Unity container with default services when the application starts.

Categories