ubuntu 16.04下用.NET Core写C#程序

来源:互联网 发布:winaip阅读器软件下载 编辑:程序博客网 时间:2024/06/13 07:41

2017 微软一个比较大的动作就是.Net core2.0了,微软出的这个框架野心很大:它可以让用户在几乎所有的PC终端用C#编写自己的应用程序。在win/linux/mac三大主流操作系统通吃之后,.Net core居然也支持Docker!
本文试着在ubuntu 16.04运行自己的C#程序,其他操作系统包括Windows,MacOS和Docker上的教程见官网:.Net Core
首先注入密钥更新系统:

sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B02C46DF417A0893sudo apt-get update

然后安装.Net SDK:

sudo apt-get install dotnet-dev-2.0 #安装最新2.0预览版

接着创建你的项目,一般第一个项目就是黑框框打印”hello world”,那我们就把项目类型指定为”console”,类似于vs中的控制台项目。

dotnet new console -o firstapp

执行完这条命令后,打印如下:

Welcome to .NET Core!---------------------Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.Telemetry--------------The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include command-line arguments. The data is collected by Microsoft and shared with the community.You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.Configuring...-------------------A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once.Decompressing 100% 5047 msExpanding 100% 8946 msGetting ready...The template "Console Application" was created successfully.Processing post-creation actions...Running 'dotnet restore' on firstapp/firstapp.csproj...Restore succeeded.

执行tree ..命令,查看项目结构,可以看到:

..└── firstapp    ├── firstapp.csproj    ├── obj    │   ├── firstapp.csproj.nuget.g.props    │   ├── firstapp.csproj.nuget.g.targets    │   └── project.assets.json    └── Program.cs

在你的Program.cs中编写你的程序,这里其实已经自动生成了”Hello World!”的打印代码。
然后执行以下命令,程序就会在终端打印”hello world”:

cd firstappdotnet restoredotnet run 

在首次运行时候会创建一个Nugut.Config文件,详细打印如下:

# dotnet restore  Restoring packages for /home/Csharp/firstapp/firstapp.csproj...  Lock file has not changed. Skipping lock file write. Path: /home/Csharp/firstapp/obj/project.assets.json  Restore completed in 93.44 ms for /home/Csharp/firstapp/firstapp.csproj.  NuGet Config files used:      /root/.nuget/NuGet/NuGet.Config  Feeds used:      https://api.nuget.org/v3/index.json      /root/.dotnet/NuGetFallbackFolder