[.NET Core] 1.Hello World

  • 445
  • 0
  • 2016-09-17

來開始嘗試第一支 .NET Core Console 程式吧!

這裡會在 Windows 環境下使用 dotnet 來執行 Console 程式,並顯示出  Hello Word!

先打開命令提示字元,用 dotnet 指令,

1.建立一個資料夾叫做 1_HelloWorld

mkdir 1_HelloWorld

2.切換到該目錄中

cd 1_helloworld

3.在這目錄裡面建立新專案

dotnet new

會看到 Program.cs 

using System;

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

與 project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

4. 第一次要還原必要套件

dotnet restore

執行後會多了 project.lock.json,以下列出部分內容

{
  "locked": false,
  "version": 2,
  "targets": {
    ".NETCoreApp,Version=v1.0": {
      "Libuv/1.9.0": {
        "type": "package",
        "dependencies": {
          "Microsoft.NETCore.Platforms": "1.0.1"
        },
        "runtimeTargets": {
          "runtimes/osx/native/_._": {
            "assetType": "native",
            "rid": "osx"
          }
        }
      },
      "Microsoft.CodeAnalysis.Analyzers/1.1.0": {
        "type": "package"
      },

5.編譯且執行程式

dotnet run

 會看到編譯結果

Project 1_HelloWorld (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling 1_HelloWorld for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.5697792

還有執行結果 

Hello World!

專案編譯後的 DLL 會放在

1_HelloWorld\bin\Debug\netcoreapp1.0\1_HelloWorld.dll
netcoreapp1.0 是因為我們在 project.json 中要建置的 frameworks 目標有 netcoreapp1.0

 6. 接著我們直接執行 1_HelloWorld.dll 試試,這次不是使用 dotnet run ,而是直接切換到 netcoreapp1.0 目錄下

cd bin\Debug\netcoreapp1.0

然後直接用 dotnet 執行程式

dotnet 1_helloworld.dll

就能看到命令提示字元上顯示

Hello World!

 

7. 如果你不想用 dotnet 指令來執行呢? 我在 Windows 平台,當然是要 exe 才爽阿,

那我們接著就到 project.json 的 frameworks 加上需要的目標 framework(參考這篇 Project.json definition dnx451 vs .dotnet ( 4.51)

我這邊就加上 dnx46(我電腦有安裝 .Net Framework 4.6 SDK ),

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "dnx46":{},
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}
加入新的 framework 後,記得用 dotnet restore 還原套件一下

 8. 然後在 \1_HelloWorld\ 這裡下指令發行專案,就會將各個 frameworks 發行出來為各平台目標的檔案,

dotnet publish

輸出訊息

d:\1_HelloWorld>dotnet publish
Publishing 1_HelloWorld for .NETCoreApp,Version=v1.0
Project 1_HelloWorld (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling 1_HelloWorld for .NETCoreApp,Version=v1.0

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:00.9054530


publish: Published to d:\1_HelloWorld\bin\Debug\netcoreapp1.0\publish
Publishing 1_HelloWorld for DNX,Version=v4.6/win7-x64
Project 1_HelloWorld (DNX,Version=v4.6) will be compiled because Input items added from last build
Compiling 1_HelloWorld for DNX,Version=v4.6

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:00.8461498


publish: Published to d:\1_HelloWorld\bin\Debug\dnx46\win7-x64\publish
Published 2/2 projects successfully

你應該會看到在 1_HelloWorld\bin\Debug\ 下多了 dnx46 目錄,裡面就有你熟悉的 1_HelloWorld.exe,

在用命令提示字元直接執行看看

\1_HelloWorld\bin\Debug\dnx46>1_helloworld

輸出結果 

Hello World!

Hello World 第一步踏出來了! 

 

參考文章

讓我們 Core 在一起:ASP.NET Core & .NET Core