一个偶然的机会,一个朋友想做一个程序,同时支持windows和linux,本来想用go来写,奈何不太熟练,突然想到.net不是也支持跨平台了吗,还没有操作过,刚好可以试验一下。
最新的.net 6已经跨平台了,从.net core 3.0后,不再叫.net core 4.0了,而是统一为.net 5,.net 6等。
演示程序如下:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!输入Q退出。");
while (true)
{
string str = Console.ReadLine();
if (str.ToLower() == "q")
{
break;
}
else
{
Console.WriteLine("输入Q退出。");
}
}
}
}
}
以下为生成文件
在windows上运行就不用说了,直接编译,点击exe就可以了。
那么在linux上怎么运行呢,这里以debian为例演示
先安装运行时环境
官方安装教程
https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian
根据操作系统版本,选择相应的教程,我这里操作系统是用和debian 10
上面是安装SDK和安装运行时的,一般开发人员可以安装SDK,用于开发使用。
普通用户,仅仅是使用的话,就用运行时就可以了。SDK是包含运行时的。
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y aspnetcore-runtime-6.0
下面有一个提示,大概意思就是说你也可以安装.net运行时,.net运行时不包含asp.net core(从这句话理解,好像是说dotnet-runtime比aspnetcore-runtime要小),安装方式就是
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-runtime-6.0
安装过程,注意我实际安装的是.net5.0,因为我用VS2019默认是.net5.0
root@debiantest1:/etc/apt/sources.list.d# sudo apt-get update; sudo apt-get install -y apt-transport-https && sudo apt-get update && sudo apt-get install -y dotnet-runtime-5.0
Hit:1 http://mirrors.ustc.edu.cn/debian stretch-updates InRelease
Hit:2 http://mirrors.ustc.edu.cn/debian stretch-backports InRelease
Ign:3 http://mirrors.ustc.edu.cn/debian stretch InRelease
Hit:4 http://mirrors.ustc.edu.cn/debian-security stretch/updates InRelease
Hit:5 http://mirrors.ustc.edu.cn/debian stretch Release
Hit:6 http://security.debian.org/debian-security buster/updates InRelease
Hit:8 http://mirrors.huaweicloud.com/debian buster InRelease
Hit:9 http://mirrors.huaweicloud.com/debian buster-updates InRelease
Hit:10 https://download.docker.com/linux/debian buster InRelease
Hit:11 https://packages.microsoft.com/debian/10/prod buster InRelease
Hit:12 http://deb.nodesource.com/node_13.x buster InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
apt-transport-https is already the newest version (1.8.2.3).
0 upgraded, 0 newly installed, 0 to remove and 141 not upgraded.
Hit:1 http://mirrors.ustc.edu.cn/debian stretch-updates InRelease
Hit:2 http://mirrors.ustc.edu.cn/debian stretch-backports InRelease
Ign:3 http://mirrors.ustc.edu.cn/debian stretch InRelease
Hit:4 http://mirrors.ustc.edu.cn/debian-security stretch/updates InRelease
Hit:5 http://mirrors.ustc.edu.cn/debian stretch Release
Hit:7 http://mirrors.huaweicloud.com/debian buster InRelease
Hit:8 http://mirrors.huaweicloud.com/debian buster-updates InRelease
Hit:9 https://download.docker.com/linux/debian buster InRelease
Hit:10 https://packages.microsoft.com/debian/10/prod buster InRelease
Hit:11 http://deb.nodesource.com/node_13.x buster InRelease
Hit:12 http://security.debian.org/debian-security buster/updates InRelease
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
dotnet-host dotnet-hostfxr-5.0 dotnet-runtime-deps-5.0
The following NEW packages will be installed:
dotnet-host dotnet-hostfxr-5.0 dotnet-runtime-5.0 dotnet-runtime-deps-5.0
0 upgraded, 4 newly installed, 0 to remove and 141 not upgraded.
Need to get 22.2 MB of archives.
After this operation, 70.8 MB of additional disk space will be used.
Get:1 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-host amd64 6.0.3-1 [55.7 kB]
Get:2 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-hostfxr-5.0 amd64 5.0.15-1 [140 kB]
Get:3 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-runtime-deps-5.0 amd64 5.0.15-1 [2,648 B]
Get:4 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-runtime-5.0 amd64 5.0.15-1 [22.0 MB]
Fetched 22.2 MB in 12s (1,849 kB/s)
Selecting previously unselected package dotnet-host.
(Reading database ... 59387 files and directories currently installed.)
Preparing to unpack .../dotnet-host_6.0.3-1_amd64.deb ...
Unpacking dotnet-host (6.0.3-1) ...
Selecting previously unselected package dotnet-hostfxr-5.0.
Preparing to unpack .../dotnet-hostfxr-5.0_5.0.15-1_amd64.deb ...
Unpacking dotnet-hostfxr-5.0 (5.0.15-1) ...
Selecting previously unselected package dotnet-runtime-deps-5.0.
Preparing to unpack .../dotnet-runtime-deps-5.0_5.0.15-1_amd64.deb ...
Unpacking dotnet-runtime-deps-5.0 (5.0.15-1) ...
Selecting previously unselected package dotnet-runtime-5.0.
Preparing to unpack .../dotnet-runtime-5.0_5.0.15-1_amd64.deb ...
Unpacking dotnet-runtime-5.0 (5.0.15-1) ...
Setting up dotnet-host (6.0.3-1) ...
Setting up dotnet-runtime-deps-5.0 (5.0.15-1) ...
Setting up dotnet-hostfxr-5.0 (5.0.15-1) ...
Setting up dotnet-runtime-5.0 (5.0.15-1) ...
Processing triggers for man-db (2.8.5-2) ...
安装后验证
root@debiantest1:/etc/apt/sources.list.d# dotnet --info
Host (useful for support):
Version: 5.0.15
Commit: 3065735be7
.NET SDKs installed:
No SDKs were found.
.NET runtimes installed:
Microsoft.NETCore.App 5.0.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
运行程序
root@debiantest1:/data/demo1# dotnet ConsoleApp1.dll
Hello World!输入Q退出。
输入Q退出。
输入Q退出。
输入Q退出。
输入Q退出。
输入Q退出。
q
可以看到已经运行成功了。和windows的效果一样。