Showing posts with label Console Application. Show all posts
Showing posts with label Console Application. Show all posts

Friday, July 20, 2018

How do you run a C# Console Application using PowerShell?

How do you run C# Console Application using PowerShell?

Microsoft.PowerShell.Utility module has a cmdlet, Add-Type. Add-Type can be used to execute the code from PowerShell command shell.

The Add-Type has a large number of parameters and using these Add-Type will define and generate the class. If you specify source code, Add-Type compiles the source code and generates an in-memory assembly that contains the .NET Framework types.

Here are some of the parameters of Add-Type from Microsoft documentation here.

Add-Type
   [-CodeDomProvider ]
   [-CompilerParameters ]
   [-TypeDefinition]
   [-Language ]
   [-ReferencedAssemblies ]
   [-OutputAssembly ]
   [-OutputType ]
   [-PassThru]
   [-IgnoreWarnings]
   []


Here is an example of using the source code in a here-string.


Now we use this string in TypeDefinition that takes a String as follows:

Add-Type -TypeDefinition $Source

Now the code is executed using the following:

[Joinstring]::Add("Jay"," Krishnaswamy"

Here is the code in Windows PowerShell ISE.

 

 

Tuesday, August 11, 2015

How do you create a Visual C++ CLR Console Application in Visual Studio 2015?

Common Language Runtime (CLR) applications depend on .NET Framework and are compiled to intermediate code interpreted by CLR at run time. Win32 applications compile/link directly to machine code and is independent of CLR.

You can create Visual C++ CLR Console application using Visual Studio.

Launch VS2015 Express 2015 for Windows Desktop.

Click File | New Project
Expand Visual C++ node and click CLR. The New Project Windows presents the diffrent project Templates as shown:


ClrConsole_01

Click CLR Console Application  shown above and change the Name at the bottom of screen to ClrConsoleApp.

Click OK to create the folder/files for the project template as shown. Accept default location for directory. The project gets created as shown:


ClrConsole_02

Make sure you read the ReadMe.txt file in the project that gives the overview of the project you created.
The References node shows the default references general, data and xml.

Reference System contains fundamental classes and bare classed that define commonly used ValueTypes and reference datatypes, events, handlers etc.

mscorlib: assembly dll that supports .NET Framework development

The template already has the 'Hello World' program. Note that for this program all that you need is the namespace System. The following is the main project file created by the template.
--
// ClrConsoleApp.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array ^args)
{
    Console::WriteLine(L"Hello World");
    return 0;
}

-------------
Click Build | Build Solution for F7 (see next image)


ClrConsole_03

 
The build succeeds as it should as shown:

ClrConsole_04

Click Debug | Start Without Debugging. The Console window opens as
shown displaying the result of running the code:

ClrConsole_05
The next image shows the project in the file system of the computer.

ClrConsole_06
Access the project properties to view more details:


ClrConsole_07
The Application.exe file can be run from command line. There is no graphical user interface.

Note that intellisense is available in the VS 2015 IDE as shown here (just added a int variable):

ClrConsole_08

Here is some code added to ClrConsoleApp.cpp
--------------
// ClrConsoleApp.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array ^args)
{
    Console::WriteLine(L"Hello World");
    Console::WriteLine("");
 int x = 5;
 Console::WriteLine(x);
 return 0;
}


The code when run produces this: