[C#][ubuntu]Ubuntu18.04安装.NET 5.0 SDK

安装命令:

wget https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

sudo apt-get install apt-transport-https
sudo apt-get update

sudo apt-get install dotnet-sdk-5.0

测试安装:

dotnet new console --output sample1

如果运行报错:可以在前面加sudo

运行结果:

Welcome to .NET 5.0!
---------------------
SDK Version: 5.0.100

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Getting ready...
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on sample1/sample1.csproj...
  Determining projects to restore...
  Restored /root/.net5/sample1/sample1.csproj (in 70 ms).
Restore succeeded.

查看项目结构
使用 tree sample1 查看目录结构,内容如下所示:

root@server00:~/.net5# tree sample1/
sample1/
├── obj
│   ├── project.assets.json
│   ├── project.nuget.cache
│   ├── sample1.csproj.nuget.dgspec.json
│   ├── sample1.csproj.nuget.g.props
│   └── sample1.csproj.nuget.g.targets
├── Program.cs
└── sample1.csproj

1 directory, 7 files

查看一下主程序 Progrom.cs 的内容如下所示:

root@server00:~/.net5# cat sample1/Program.cs
using System;

namespace sample1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
编译并运行
现在使用命令 dotnet run --project sample1 进行编译并运行,可以看到以下内容:

root@server00:~/.net5# dotnet run --project sample1
Hello World!

运行成功。

然后我们再看一下项目的结构,可以看到出很多新的内容:

root@server00:~/.net5# tree sample1/
sample1/
├── bin
│   └── Debug
│       └── net5.0
│           ├── ref
│           │   └── sample1.dll
│           ├── sample1
│           ├── sample1.deps.json
│           ├── sample1.dll
│           ├── sample1.pdb
│           ├── sample1.runtimeconfig.dev.json
│           └── sample1.runtimeconfig.json
├── obj
│   ├── Debug
│   │   └── net5.0
│   │       ├── apphost
│   │       ├── ref
│   │       │   └── sample1.dll
│   │       ├── sample1.AssemblyInfo.cs
│   │       ├── sample1.AssemblyInfoInputs.cache
│   │       ├── sample1.assets.cache
│   │       ├── sample1.csprojAssemblyReference.cache
│   │       ├── sample1.csproj.CoreCompileInputs.cache
│   │       ├── sample1.csproj.FileListAbsolute.txt
│   │       ├── sample1.dll
│   │       ├── sample1.GeneratedMSBuildEditorConfig.editorconfig
│   │       ├── sample1.genruntimeconfig.cache
│   │       └── sample1.pdb
│   ├── project.assets.json
│   ├── project.nuget.cache
│   ├── sample1.csproj.nuget.dgspec.json
│   ├── sample1.csproj.nuget.g.props
│   └── sample1.csproj.nuget.g.targets
├── Program.cs
└── sample1.csproj

8 directories, 26 files
关于运行
其中,目录 /sample1/bin/Debug/net5.0/sample1 就是可执行程序,可以直接运行。比如:

root@server00:~/.net5# ./sample1/bin/Debug/net5.0/sample1
Hello World!

不过他还依赖于 /sample1/bin/Debug/net5.0/ref/sample1.dll 才可以运行。需要将其放置在指定的路径中,然后配置环境变量即可。
经测试,发现程序一共需要以下三个程序(放在同一目录中)才可运行:sample1、sample1.dll 和 sample1.runtimeconfig.json。其中最后一个为配置文件,内容如下:

root@server00:~/test1# cat sample1.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.0"
    }
  }
}

由此可见,此文件主要用于指定程序的类型和运行环境。

小结
通过本示例,可以看到安装和运行.NET5.0的程序还是很方便的,只需要几条命令即可。测试过程中也很顺利,几条命令运行完成,都得到了预期的效果。这样我们就可以使用.NET5.0来建立程序,可以看到
原文链接:https://blog.csdn.net/weixin_43145361/article/details/110526948

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1623863129

你的打赏是我写文章最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值