Programming

How to develop C# console application with command

I needed develop C# console application on Windows without Visual Studio and VSCode’s C# Dev Kit. But, I had a bit hard time finding information about it at that time. So I’m writing this post as a personal note for future reference.

Environment Setting

  1. Creating Windows EC2 instance on AWS.
    • I tested “Microsoft Windows 2025 Datacenter edition. [English]” (AMI: ami-042b01a80943e830b).
  2. Installing .NET (Windows x64).
  3. Installing VSCode for editing codes.

Creating Console Application and Build

1. Creating a folder and open it with VSCode.

2. Executing dotnet new console --use-program-main command to initialize a project.
Like following folder and files are generated by the command. (Project root folder name is “csharp_console_simple”)

 .
├── Program.cs
├── csharp_console_simple.csproj
└── obj
    ├── csharp_console_simple.csproj.nuget.dgspec.json
    ├── csharp_console_simple.csproj.nuget.g.props
    ├── csharp_console_simple.csproj.nuget.g.targets
    ├── project.assets.json
    └── project.nuget.cache

2 directories, 7 files

3. If you want to change target framework version, changing TragetFramework in .csproj file to like “net6.0”.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

4. Editing Program.cs.
Default Program.cs content is the following:

namespace csharp_console_simple;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
    }
}

5. Executing dotnet build --configuration Release command. Adding “–framework net6.0” option if you want to specify target framework.

6. Launching exe file.
If you installed .NET 8 and target framework is net6.0, you will see “You must install or update .NET to run this application.” error. In that case, please follow the next step.

> .\bin\Release\net8.0\csharp_console_simple.exe
Hello, World!

7. Executing dotnet publish --runtime win-x64 -o C:\path\to\output\folder command and launching generated exe file.

> C:\path\to\output\folder\csharp_console_simple.exe
Hello, World!

How to Install Library

You can install a library by executing dotnet add package command, and the command adds dependency info to .csproj.

dotnet add package log4net -v 2.0.17

How to Add DLL

You can add DLL which cannot be installed by NuGet to your project with editing .csproj like following.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="xxxxx">
      <HintPath>.\path\to\folder\xxxxx.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>

zuqqhi2

Share
Published by
zuqqhi2
Tags: dotnet