一个偶然的机会,一个朋友想做一个程序,同时支持windows和linux,本来想用go来写,奈何不太熟练,突然想到.net不是也支持跨平台了吗,还没有操作过,刚好可以试验一下。

最新的.net 6已经跨平台了,从.net core 3.0后,不再叫.net core 4.0了,而是统一为.net 5,.net 6等。

演示程序如下:

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ConsoleApp1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. Console.WriteLine("Hello World!输入Q退出。");
  14. while (true)
  15. {
  16. string str = Console.ReadLine();
  17. if (str.ToLower() == "q")
  18. {
  19. break;
  20. }
  21. else
  22. {
  23. Console.WriteLine("输入Q退出。");
  24. }
  25. }
  26. }
  27. }
  28. }

以下为生成文件



在windows上运行就不用说了,直接编译,点击exe就可以了。

那么在linux上怎么运行呢,这里以debian为例演示

先安装运行时环境

官方安装教程

https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian

根据操作系统版本,选择相应的教程,我这里操作系统是用和debian 10

上面是安装SDK和安装运行时的,一般开发人员可以安装SDK,用于开发使用。

普通用户,仅仅是使用的话,就用运行时就可以了。SDK是包含运行时的。

  1. sudo apt-get update; \
  2. sudo apt-get install -y apt-transport-https && \
  3. sudo apt-get update && \
  4. sudo apt-get install -y aspnetcore-runtime-6.0

下面有一个提示,大概意思就是说你也可以安装.net运行时,.net运行时不包含asp.net core(从这句话理解,好像是说dotnet-runtime比aspnetcore-runtime要小),安装方式就是

  1. sudo apt-get update; \
  2. sudo apt-get install -y apt-transport-https && \
  3. sudo apt-get update && \
  4. sudo apt-get install -y dotnet-runtime-6.0

安装过程,注意我实际安装的是.net5.0,因为我用VS2019默认是.net5.0

  1. 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
  2. Hit:1 http://mirrors.ustc.edu.cn/debian stretch-updates InRelease
  3. Hit:2 http://mirrors.ustc.edu.cn/debian stretch-backports InRelease
  4. Ign:3 http://mirrors.ustc.edu.cn/debian stretch InRelease
  5. Hit:4 http://mirrors.ustc.edu.cn/debian-security stretch/updates InRelease
  6. Hit:5 http://mirrors.ustc.edu.cn/debian stretch Release
  7. Hit:6 http://security.debian.org/debian-security buster/updates InRelease
  8. Hit:8 http://mirrors.huaweicloud.com/debian buster InRelease
  9. Hit:9 http://mirrors.huaweicloud.com/debian buster-updates InRelease
  10. Hit:10 https://download.docker.com/linux/debian buster InRelease
  11. Hit:11 https://packages.microsoft.com/debian/10/prod buster InRelease
  12. Hit:12 http://deb.nodesource.com/node_13.x buster InRelease
  13. Reading package lists... Done
  14. Reading package lists... Done
  15. Building dependency tree
  16. Reading state information... Done
  17. apt-transport-https is already the newest version (1.8.2.3).
  18. 0 upgraded, 0 newly installed, 0 to remove and 141 not upgraded.
  19. Hit:1 http://mirrors.ustc.edu.cn/debian stretch-updates InRelease
  20. Hit:2 http://mirrors.ustc.edu.cn/debian stretch-backports InRelease
  21. Ign:3 http://mirrors.ustc.edu.cn/debian stretch InRelease
  22. Hit:4 http://mirrors.ustc.edu.cn/debian-security stretch/updates InRelease
  23. Hit:5 http://mirrors.ustc.edu.cn/debian stretch Release
  24. Hit:7 http://mirrors.huaweicloud.com/debian buster InRelease
  25. Hit:8 http://mirrors.huaweicloud.com/debian buster-updates InRelease
  26. Hit:9 https://download.docker.com/linux/debian buster InRelease
  27. Hit:10 https://packages.microsoft.com/debian/10/prod buster InRelease
  28. Hit:11 http://deb.nodesource.com/node_13.x buster InRelease
  29. Hit:12 http://security.debian.org/debian-security buster/updates InRelease
  30. Reading package lists... Done
  31. Reading package lists... Done
  32. Building dependency tree
  33. Reading state information... Done
  34. The following additional packages will be installed:
  35. dotnet-host dotnet-hostfxr-5.0 dotnet-runtime-deps-5.0
  36. The following NEW packages will be installed:
  37. dotnet-host dotnet-hostfxr-5.0 dotnet-runtime-5.0 dotnet-runtime-deps-5.0
  38. 0 upgraded, 4 newly installed, 0 to remove and 141 not upgraded.
  39. Need to get 22.2 MB of archives.
  40. After this operation, 70.8 MB of additional disk space will be used.
  41. Get:1 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-host amd64 6.0.3-1 [55.7 kB]
  42. Get:2 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-hostfxr-5.0 amd64 5.0.15-1 [140 kB]
  43. 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]
  44. Get:4 https://packages.microsoft.com/debian/10/prod buster/main amd64 dotnet-runtime-5.0 amd64 5.0.15-1 [22.0 MB]
  45. Fetched 22.2 MB in 12s (1,849 kB/s)
  46. Selecting previously unselected package dotnet-host.
  47. (Reading database ... 59387 files and directories currently installed.)
  48. Preparing to unpack .../dotnet-host_6.0.3-1_amd64.deb ...
  49. Unpacking dotnet-host (6.0.3-1) ...
  50. Selecting previously unselected package dotnet-hostfxr-5.0.
  51. Preparing to unpack .../dotnet-hostfxr-5.0_5.0.15-1_amd64.deb ...
  52. Unpacking dotnet-hostfxr-5.0 (5.0.15-1) ...
  53. Selecting previously unselected package dotnet-runtime-deps-5.0.
  54. Preparing to unpack .../dotnet-runtime-deps-5.0_5.0.15-1_amd64.deb ...
  55. Unpacking dotnet-runtime-deps-5.0 (5.0.15-1) ...
  56. Selecting previously unselected package dotnet-runtime-5.0.
  57. Preparing to unpack .../dotnet-runtime-5.0_5.0.15-1_amd64.deb ...
  58. Unpacking dotnet-runtime-5.0 (5.0.15-1) ...
  59. Setting up dotnet-host (6.0.3-1) ...
  60. Setting up dotnet-runtime-deps-5.0 (5.0.15-1) ...
  61. Setting up dotnet-hostfxr-5.0 (5.0.15-1) ...
  62. Setting up dotnet-runtime-5.0 (5.0.15-1) ...
  63. Processing triggers for man-db (2.8.5-2) ...

安装后验证

  1. root@debiantest1:/etc/apt/sources.list.d# dotnet --info
  2. Host (useful for support):
  3. Version: 5.0.15
  4. Commit: 3065735be7
  5. .NET SDKs installed:
  6. No SDKs were found.
  7. .NET runtimes installed:
  8. Microsoft.NETCore.App 5.0.15 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  9. To install additional .NET runtimes or SDKs:
  10. https://aka.ms/dotnet-download

运行程序

  1. root@debiantest1:/data/demo1# dotnet ConsoleApp1.dll
  2. Hello World!输入Q退出。
  3. 输入Q退出。
  4. 输入Q退出。
  5. 输入Q退出。
  6. 输入Q退出。
  7. 输入Q退出。
  8. q

可以看到已经运行成功了。和windows的效果一样。

.net跨平台运行实践的更多相关文章

  1. ASP.NET跨平台最佳实践

    前言 八年的坚持敌不过领导的固执,最终还是不得不阔别已经成为我第二语言的C#,转战Java阵营.有过短暂的失落和迷茫,但技术转型真的没有想象中那么难.回头审视,其实单从语言本身来看,C#确实比Java ...

  2. 开源一个跨平台运行的服务插件 - TaskCore.MainForm

    本次将要很大家分享的是一个跨平台运行的服务插件 - TaskCore.MainForm,此框架是使用.netcore来写的,现在netcore已经支持很多系统平台运行了,所以将以前的Task.Main ...

  3. cross-env 兼容跨平台运行脚本

    https://www.npmjs.com/package/cross-env 跨平台运行脚本 The problem Most Windows command prompts will choke ...

  4. 物联网框架ServerSuperIO在.NetCore实现跨平台的实践路线

    正所谓天下大势,不跟风不行.你不跨平台,很low嘛.java说:你们能跨嘛,跨给我看看.C#说:不要强人所难嘛.java说:能部署在云上吗?docker?微服务?C#说:不要强人所难嘛.java说:你 ...

  5. Flutter 实现原理及在马蜂窝的跨平台开发实践

    一直以来,跨平台开发都是困扰移动客户端开发的难题. 在马蜂窝旅游 App 很多业务场景里,我们尝试过一些主流的跨平台开发解决方案, 比如 WebView 和 React Native,来提升开发效率和 ...

  6. 跨平台技术实践案例: 用 reactxp 重写墨刀的移动端

    Authors:  Gao Cong, Perry Poon Illustrators:  Shena Bian April 20, 2019 重新编写,又一次,我们又一次重新编写了移动端应用和移动端 ...

  7. 跨平台运行 Rafy 首次部署记录

    一直想在 Linux 上使用 MONO 试试运行 Rafy,最近因为业务需要,总算是真正地试验了一次.下面是本次部署记录的一些要点. Linux 这次部署,我是和两位同事一起来试验的.由于我们对 Li ...

  8. 跨平台运行ASP.NET Core 1.0

    前言 首先提一下微软更名后的叫法: ASP.NET 5 更名为 ASP.NET Core 1.0 .NET Core 更名为 .NET Core 1.0 Entity Framework 7 更名为  ...

  9. Java为什么能跨平台运行

    因为它有虚拟机(JVM),JAVA程序不是直接在电脑上运行的,是在虚拟机上进行的,每个系统平台都是有自己的虚拟机(JVM),所以JAVA语言能跨平台. 1, java代码不是直接运行在CPU上,而是运 ...

  10. 跨平台运行ASP.NET Core 1.0(转载)

    前言 首先提一下微软更名后的叫法: ASP.NET 5 更名为 ASP.NET Core 1.0 .NET Core 更名为 .NET Core 1.0 Entity Framework 7 更名为  ...

随机推荐

  1. Google C++ 风格指南记录

    最近在看谷歌的 C++ 风格指南发现了一些有意思的知识点,遂记录下 1. 第六章第二小节介绍了右值引用 只在定义移动构造函数与移动赋值操作时使用右值引用. 不要使用 std::forward. 定义: ...

  2. Puppeteer介绍

    Puppeteer是什么 Puppeteer是一个Node库,它提供了一个高级API来通过DevTools协议控制Chromium或Chrome. 可以使用Puppeteer来自动化完成浏览器的操作, ...

  3. win终端利器-Cmder的安装使用

    cmder 官网:https://cmder.app/ 安装 直接选择full版本下载,完成后解压即可 启动 直接双击Cmder.exe 如果每次都进入到 Cmder 解压目录双击 Cmder.exe ...

  4. 2024-02-28:用go语言,有一个由x轴和y轴组成的坐标系, “y下“和“y上“表示一条无限延伸的道路,“y下“表示这个道路的下限,“y上“表示这个道路的上限, 给定一批长方形,每一个长方形有(

    2024-02-28:用go语言,有一个由x轴和y轴组成的坐标系, "y下"和"y上"表示一条无限延伸的道路,"y下"表示这个道路的下限,& ...

  5. 关于 LLM 和知识图谱、图数据库,大家都关注哪些问题呢?

    自 LLM 系列文章<知识图谱驱动的大语言模型 Llama Index>.<Text2Cypher:大语言模型驱动的图查询生成>.<Graph RAG: 知识图谱结合 L ...

  6. 别再低效筛选数据了!试试pandas query函数

    数据过滤在数据分析过程中具有极其重要的地位,因为在真实世界的数据集中,往往存在重复.缺失或异常的数据.pandas提供的数据过滤功能可以帮助我们轻松地识别和处理这些问题数据,从而确保数据的质量和准确性 ...

  7. Toyota Programming Contest 2024#2(AtCoder Beginner Contest 341)D - Only one of two(数论、二分)

    目录 链接 题面 题意 题解 代码 总结 链接 D - Only one of two 题面 题意 求第\(k\)个只能被\(N\)或\(M\)整除的数 题解 \([1,x]\)中的能被\(n\)整除 ...

  8. SelectZenEmpty 下拉框 支持 最大长度 超出... vue 组件

    <template> <Select v-model="innerValue" :disabled="disabled" :clearable ...

  9. 2.String类能被继承吗

    2.String类能被继承吗 不可以,因为String类有final修饰符,而final修饰的类是不能被继承的. 拓展 String的底层是一个用private和final修饰的char数组.fina ...

  10. windows10 使用gcc编译生成可执行文件exe实例解析

    一 操作步骤 1.生成可执行程序 cd xxx # 先进入源程序所在的目录 gcc hello.cpp # 一次性编译,windows系统生成a.exe文件,Linux系统生成a.out文件 gcc ...