C# Visual Studio Code: Building a Hello World executable

11,383

Go to [program_name]>bin>debug>[the_only_foler_in_there] the executable should be there

Share:
11,383
DisplayName001
Author by

DisplayName001

Updated on June 17, 2022

Comments

  • DisplayName001
    DisplayName001 almost 2 years

    I am attempting to build a simple hello world executable in Visual Studio Code and don't know what I am doing incorrect? and to explain I will go over my steps.

    To start I was following the simple tutorial here https://docs.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code

    Once I got down to the bottom I thought I would take my C# project and compile(or build) the project. Well I guess they don't go over this near the beginning because it's not real straight forward.

    First of all I had to figure out that Visual Studio and Visual Studio Code do not share the same knowledgebase (at least when it comes to compiling).

    The next thing is that visual studio code uses Tasks to build the executable, well at least that is what I got from this How do you compile a console application with VS Code (Windows platform)?

    So what I did was

    Step 1) Create new folder in windows explorer where I wanted my project to reside
    Step 2) Open up visual studio and using the terminal navigate to the folder
    Step 3) Type the command >>dotnet new console
    Step 4) Type the command >>dotnet restore
    Step 5) Make sure my code looks like

    using System; //The using keyword is used to include the system namespace in the program
    
    namespace HelloWorldApplication //A namespace is a collection of classes
    {
        class HelloWorld
    {
            static void Main(string[] args)
            {
                Console.WriteLine("Hello World!");
                Console.ReadKey();
           }
        }
    }
    

    After that I pressed F1 typed in "Run Build Task"

    A prompt asking me to create a build task where I selected .NET core creating a tasks.json file

    {
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet build",
            "type": "shell",
            "group": "build",
            "presentation": {
                "reveal": "silent"
            },
            "problemMatcher": "$msCompile"
        }
    ]
    }
    

    Now I press ctrl+shift+b select build and well nothing happens.

    I went down to the terminal and typed dotnet build and got the following terminal response

    d:\VS_Sandbox\HelloWorldApplication>dotnet build
    Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Restoring packages for d:\VS_Sandbox\HelloWorldApplication\HelloWorldApplication.csproj...
      Generating MSBuild file d:\VS_Sandbox\HelloWorldApplication\obj\HelloWorldApplication.csproj.nuget.g.props.
      Restore completed in 134.6 ms for d:\VS_Sandbox\HelloWorldApplication\HelloWorldApplication.csproj.
      HelloWorldApplication -> d:\VS_Sandbox\HelloWorldApplication\bin\Debug\netcoreapp2.2\HelloWorldApplication.dll
    
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    
    Time Elapsed 00:00:01.03
    

    To which I go and look for my brand new executable and well nothing? I attempted to go take a look at the help on microsoft here https://code.visualstudio.com/docs/editor/tasks

    but thought I was going to find how to use csc.exe in my tasks but I don't know how and I am not even sure I am supposed too?

    If anyone knows what I am doing wrong please let me know.

  • Hobbamok
    Hobbamok almost 5 years
    A DLL is NOT an executeable! That's the problem.
  • Martin Liversage
    Martin Liversage about 3 years
    To run HelloWorldApplication.dll you have to use the dotnet.exe application. That's why you have to execute dotnet HelloWorldApplication.dll as stated in this answer.