public static void GetRegistData() { string name = "huishuangzhu"; //搜索到註冊表根目錄 RegistryKey hkml = Registry.ClassesRoot; //搜索到註冊表根目錄下的XXX文件夾。 RegistryK ...
作者:
目錄目錄
在前一篇文章(在亞馬遜AWS上創建ARM伺服器並配置好RDP遠程桌面連接的心得)里,我們成功在亞馬遜AWS上創建了Ubuntu系統的ARM伺服器。現在準備在該伺服器上安裝 .NET SDK。
一、嘗試 apt-get 安裝
首先嘗試了之前在 x86 處理器的Ubuntu系統上安裝 .NET SDK的經驗,使用 apt-get 來安裝。
ubuntu@ip-175-76-22-54:~$ sudo apt-get install -y dotnet6
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package dotnet6
遇到錯誤,找不到“dotnet6”的安裝包。隨後換了幾個版本也不行,sudo apt-get update
也沒效果。
於是查找了一下官方文檔,發現 《在 Ubuntu 上安裝 .NET SDK 或 .NET 運行時》中有這一段話:
僅在 x64 體繫結構上支持包管理器安裝。 對於 Arm 等其他體繫結構,必須通過其他一些方式安裝 .NET,例如,通過 Snap 和安裝程式腳本進行安裝,或通過手動提取二進位文件進行安裝。
原來ARM體系不支持包管理安裝,看來得換一種辦法來安裝了。
二、嘗試 snap 安裝
現在來嘗試 snap 安裝。
摘錄官方文檔:
若要安裝適用於 .NET SDK 的 Snap 包,請運行 snap install 命令。 使用 --channel 參數來指明要安裝哪個版本。 如果省略此參數,則使用 latest/stable。 在下麵的示例中,指定的是 7.0:
Bash
sudo snap install dotnet-sdk --classic --channel=7.0
隨後參考它,在我們的ARM伺服器上執行命令。
ubuntu@ip-175-76-22-54:~$ sudo snap install dotnet-sdk --classic --channel=7.0
error: snap "dotnet-sdk" is not available on 7.0/stable but other tracks exist.
Please be mindful that different tracks may include different features.
Get more information with 'snap info dotnet-sdk'.
有報錯,說找不到可用的安裝包。
於是用命令查詢了一下 snap 上有哪些 dotnet-sdk 版本。
ubuntu@ip-175-76-22-54:~$ snap info dotnet-sdk
name: dotnet-sdk
summary: Develop high performance applications in less time, on any platform.
publisher: Microsoft .NET Core (dotnetcore✓)
store-url: https://snapcraft.io/dotnet-sdk
contact: https://dot.net/core
license: MIT
description: |
.NET Core is the modular and high performance implementation of .NET for
creating web applications and services that run on Windows, Linux and Mac.
It is open source and it can share the same code with .NET Framework and
Xamarin apps.
.NET Core is a .NET Foundation project. https://dotnetfoundation.org/
snap-id: uHc4y9lWxyqYfxsqcr4xILzAai4L1BHs
channels:
5.0/stable: –
5.0/candidate: –
5.0/beta: –
5.0/edge: 5.0.100-preview.3.20216.6 2020-04-21 (79) 127MB classic
發現它不支持 .net 7.0,僅有 .net 5.0版的。且沒有穩定版(stable),只在edge通道有一個預覽版(preview)。
故再換一種辦法吧。
三、使用 dotnet-install
腳本安裝
3.1 腳本準備
摘自官方文檔:
腳本安裝
dotnet-install 腳本用於 SDK 和運行時的自動化和非管理員安裝。 可通過 https://dot.net/v1/dotnet-install.sh 下載腳本。
重要:需要 Bash 才能運行該腳本。
運行此腳本之前,需要授予此腳本作為可執行文件運行的許可權:
Bash
sudo chmod +x ./dotnet-install.sh
上文介紹了腳本的網址,於是我們可以用 wget
命令下載該腳本,然後參考它修改文件屬性。
ubuntu@ip-175-76-22-54:~$ wget https://dot.net/v1/dotnet-install.sh
--2022-11-26 08:28:01-- https://dot.net/v1/dotnet-install.sh
Resolving dot.net (dot.net)... 20.53.203.50, 20.112.52.29, 20.103.85.33, ...
Connecting to dot.net (dot.net)|20.53.203.50|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh [following]
--2022-11-26 08:28:02-- https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
Resolving dotnet.microsoft.com (dotnet.microsoft.com)... 13.107.246.46, 13.107.213.46, 2620:1ec:46::46, ...
Connecting to dotnet.microsoft.com (dotnet.microsoft.com)|13.107.246.46|:443... connected.
HTTP request sent, awaiting response... 200 OK
Cookie coming from dotnet.microsoft.com attempted to set domain to dotnetwebsite.azurewebsites.net
Cookie coming from dotnet.microsoft.com attempted to set domain to dotnetwebsite.azurewebsites.net
Length: 58293 (57K) [application/x-sh]
Saving to: ‘dotnet-install.sh’
dotnet-install.sh 100%[===================>] 56.93K --.-KB/s in 0.003s
2022-11-26 08:28:02 (20.4 MB/s) - ‘dotnet-install.sh’ saved [58293/58293]
ubuntu@ip-175-76-22-54:~$ sudo chmod +x ./dotnet-install.sh
3.2 安裝最新長期支持 (LTS)版 .NET 6.0
對於安裝最新的長期支持 (LTS),可參考官方文檔:
該腳本預設安裝最新的 長期支持 (LTS) SDK 版本,即 .NET 6。 若要安裝最新版本(可能不是 LTS) 版本的 (版本),請使用 --version latest 參數。
Bash
./dotnet-install.sh --version latest
註意它在運行腳本時,沒有加“sudo”,表示該腳本支持將程式安裝到當前用戶的目錄下,無需管理員許可權。
但這對多用戶使用時會帶來麻煩。為了避免每個用戶都裝一次,於是也可選擇用“sudo”來安裝,這樣便會安裝在所有用戶都能訪問的路徑中。
ubuntu@ip-175-76-22-54:~$ sudo ./dotnet-install.sh
dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.
dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.403/dotnet-sdk-6.0.403-linux-arm64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.403/dotnet-sdk-6.0.403-linux-arm64.tar.gz
dotnet-install: Installed version is 6.0.403
dotnet-install: Adding to current process PATH: `/root/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.
從上面的信息可看出,已安裝好 .NET SDK 6.0,它裝在 /root/.dotnet
目錄中。
3.3 安裝最新版本 .NET 7.0
對於安裝指定的版本,可參考官方文檔:
可以使用 參數安裝特定的主版本 --channel ,以指示特定版本。 以下命令安裝 .NET 7.0 SDK。
Bash
./dotnet-install.sh --channel 7.0
按照文檔,進行安裝。
ubuntu@ip-175-76-22-54:~$ sudo ./dotnet-install.sh --channel 7.0
dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.
dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Sdk/7.0.100/dotnet-sdk-7.0.100-linux-arm64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/7.0.100/dotnet-sdk-7.0.100-linux-arm64.tar.gz
dotnet-install: Installed version is 7.0.100
dotnet-install: Adding to current process PATH: `/root/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.
.NET SDK 7.0 安裝成功。
3.4 安裝舊版本 .NET Core 3.1
官方文檔的“受支持”列表裡還有“.NET Core 3.1”,於是也可安裝。
ubuntu@ip-175-76-22-54:~$ sudo ./dotnet-install.sh --channel 3.1
dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.
dotnet-install: Attempting to download using primary link https://dotnetcli.azureedge.net/dotnet/Sdk/3.1.425/dotnet-sdk-3.1.425-linux-arm64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/3.1.425/dotnet-sdk-3.1.425-linux-arm64.tar.gz
dotnet-install: Installed version is 3.1.425
dotnet-install: Adding to current process PATH: `/root/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.
.NET Core SDK 3.1 安裝成功。
3.5 安裝不受支持的版本 .NET 5.0
官方文檔中,“.NET 5.0”在“不受支持”列表裡,它會否能安裝呢?於是我嘗試了一下。
ubuntu@ip-175-76-22-54:~$ sudo ./dotnet-install.sh --channel 5.0
dotnet-install: Note that the intended use of this script is for Continuous Integration (CI) scenarios, where:
dotnet-install: - The SDK needs to be installed without user interaction and without admin rights.
dotnet-install: - The SDK installation doesn't need to persist across multiple CI runs.
dotnet-install: To set up a development environment or to run apps, use installers rather than this script. Visit https://dotnet.microsoft.com/download to get the installer.
dotnet-install: Attempting to download using aka.ms link https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.408/dotnet-sdk-5.0.408-linux-arm64.tar.gz
dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.408/dotnet-sdk-5.0.408-linux-arm64.tar.gz
dotnet-install: Installed version is 5.0.408
dotnet-install: Adding to current process PATH: `/root/.dotnet`. Note: This change will be visible only when sourcing script.
dotnet-install: Note that the script does not resolve dependencies during installation.
dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section.
dotnet-install: Installation finished successfully.
.NET SDK 5.0 安裝成功。
看來“不受支持”僅是指沒有技術支持,還是可以用 dotnet-install
腳本安裝該版本的。
3.6 查看.NET SDK信息
裝好了,想查看一下安裝的SDK信息,於是執行了以下命令。
ubuntu@ip-175-76-22-54:~$ dotnet --list-sdks
dotnet: command not found
報錯了,找不到命令。
原來是它沒被自動加到環境變數,故需要寫全路徑。
ubuntu@ip-175-76-22-54:~$ /root/.dotnet/dotnet --list-sdks
-bash: /root/.dotnet/dotnet: Permission denied
這次遇到了“Permission denied”報錯。
看來是跟先前的“sudo”安裝有關,需要使用“sudo”才能執行它。
ubuntu@ip-175-76-22-54:~$ sudo /root/.dotnet/dotnet --list-sdks
3.1.425 [/root/.dotnet/sdk]
5.0.408 [/root/.dotnet/sdk]
6.0.403 [/root/.dotnet/sdk]
7.0.100 [/root/.dotnet/sdk]
ubuntu@ip-175-76-22-54:~$ sudo /root/.dotnet/dotnet --info
.NET SDK:
Version: 7.0.100
Commit: e12b7af219
Runtime Environment:
OS Name: ubuntu
OS Version: 22.04
OS Platform: Linux
RID: ubuntu.22.04-arm64
Base Path: /root/.dotnet/sdk/7.0.100/
Host:
Version: 7.0.0
Architecture: arm64
Commit: d099f075e4
.NET SDKs installed:
3.1.425 [/root/.dotnet/sdk]
5.0.408 [/root/.dotnet/sdk]
6.0.403 [/root/.dotnet/sdk]
7.0.100 [/root/.dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.31 [/root/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [/root/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.11 [/root/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0 [/root/.dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.31 [/root/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [/root/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [/root/.dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0 [/root/.dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
None
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
成功得到了安裝信息。
四、運行 .NET 程式
在Windows系統中,可方便的雙擊運行 .NET 程式;或是在命令行里直接輸入程式的exe文件名,來運行程式。
而在非Windows系統中,需要通過 dotnet 命令來運行 .NET 程式。且命令行參數里不是傳遞主程式的exe文件名,而應該傳遞主程式的dll文件名。
詳見官方文檔,摘錄:
運行應用程式:
.NET CLI
dotnet [--additionalprobingpath <PATH>] [--additional-deps <PATH>]
[--fx-version <VERSION>] [--roll-forward <SETTING>]
<PATH_TO_APPLICATION> [arguments]
dotnet exec [--additionalprobingpath] [--additional-deps <PATH>]
[--depsfile <PATH>]
[--fx-version <VERSION>] [--roll-forward <SETTING>]
[--runtimeconfig <PATH>]
<PATH_TO_APPLICATION> [arguments]
4.1 運行 .NET 7.0 程式
可以在其他電腦上開發程式,然後將編譯後的文件,拷貝到ARM伺服器上再運行。平臺(Platform)選 AnyCPU 就行,無需特意選 ARM64。且不用發佈(Publish),直接將“bin”文件夾下的 Debug或Release目錄 拷貝過去就行。
於是我將前段時間開發的 VectorClassDemo 程式拷貝了過去,順便能檢查一下 .NET 對ARM的內在函數的支持性。將該程式分別用 .NET 5.0/7.0編譯一下,然後拷貝過去。
若想將文件拷貝到遠程的伺服器上,可以用 scp 等命令。或者可以用RDP遠程桌面連接過去,在本機複製文件,然後在遠程桌面的文件管理器中粘貼,便能將文件複製過去。
文件複製成功後,進入程式所在目錄,然後便可以用 dotnet 命令來執行程式了。例如我的這個程式的主程式的dll文件名為“VectorClassDemo50.dll”,於是語句為“sudo /root/.dotnet/dotnet ./VectorClassDemo50.dll”。
輸出信息摘錄如下:
ubuntu@ip-175-76-22-54:~/Documents/VectorClassDemo50/net7.0$ sudo /root/.dotnet/dotnet ./VectorClassDemo50.dll
VectorClassDemo50
IsRelease: True
EnvironmentVariable(PROCESSOR_IDENTIFIER):
Environment.ProcessorCount: 2
Environment.Is64BitOperatingSystem: True
Environment.Is64BitProcess: True
Environment.OSVersion: Unix 5.15.0.1023
Environment.Version: 7.0.0
RuntimeEnvironment.GetRuntimeDirectory: /root/.dotnet/shared/Microsoft.NETCore.App/7.0.0/
RuntimeInformation.FrameworkDescription: .NET 7.0.0
BitConverter.IsLittleEndian: True
IntPtr.Size: 8
Vector.IsHardwareAccelerated: True
Vector<byte>.Count: 16 # 128bit
Vector<T>.Assembly.CodeBase: file:///root/.dotnet/shared/Microsoft.NETCore.App/7.0.0/System.Private.CoreLib.dll
[Intrinsics.Arm]
AdvSimd.IsSupported: True
AdvSimd.Arm64.IsSupported: True
Aes.IsSupported: True
Aes.Arm64.IsSupported: True
ArmBase.IsSupported: True
ArmBase.Arm64.IsSupported: True
Crc32.IsSupported: True
Crc32.Arm64.IsSupported: True
Dp.IsSupported: True
Dp.Arm64.IsSupported: True
Rdm.IsSupported: True
Rdm.Arm64.IsSupported: True
Sha1.IsSupported: True
Sha1.Arm64.IsSupported: True
Sha256.IsSupported: True
Sha256.Arm64.IsSupported: True
-- Single, Vector<Single>.Count=4 --
srcT: <-3.4028235E+38, Infinity, NaN, -1.2> # (FF7FFFFF 7F800000 FFC00000 BF99999A)
srcAllOnes: <NaN, NaN, NaN, NaN> # (FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF)
Abs(srcT): <3.4028235E+38, Infinity, NaN, 1.2> # (7F7FFFFF 7F800000 7FC00000 3F99999A)
可以看到:
- 該程式正確的以“64位 .NET 7.0 程式”的方式在運行。
- 向量類型支持硬體加速(Vector.IsHardwareAccelerated),且向量類型的長度為 128bit。這是因為ARM的 AdvSimd 指令集的數據長度最長為128位。
- 目前的所有ARM內在函數(Intrinsics.Arm)的IsSupported都是true,表示都支持。
4.2 運行 .NET 5.0 程式
隨後又嘗試運行了 .NET 5.0編譯的程式。
ubuntu@ip-175-76-22-54:~/Documents/VectorClassDemo50/net5.0$ sudo /root/.dotnet/dotnet ./VectorClassDemo50.dll
VectorClassDemo50
IsRelease: True
EnvironmentVariable(PROCESSOR_IDENTIFIER):
Environment.ProcessorCount: 2
Environment.Is64BitOperatingSystem: True
Environment.Is64BitProcess: True
Environment.OSVersion: Unix 5.15.0.1023
Environment.Version: 5.0.17
RuntimeEnvironment.GetRuntimeDirectory: /root/.dotnet/shared/Microsoft.NETCore.App/5.0.17/
RuntimeInformation.FrameworkDescription: .NET 5.0.17
BitConverter.IsLittleEndian: True
IntPtr.Size: 8
Vector.IsHardwareAccelerated: True
Vector<byte>.Count: 16 # 128bit
Vector<T>.Assembly.CodeBase: file:///root/.dotnet/shared/Microsoft.NETCore.App/5.0.17/System.Private.CoreLib.dll
[Intrinsics.Arm]
AdvSimd.IsSupported: True
AdvSimd.Arm64.IsSupported: True
Aes.IsSupported: True
Aes.Arm64.IsSupported: True
ArmBase.IsSupported: True
ArmBase.Arm64.IsSupported: True
Crc32.IsSupported: True
Crc32.Arm64.IsSupported: True
Dp.IsSupported: False
Dp.Arm64.IsSupported: False
Rdm.IsSupported: False
Rdm.Arm64.IsSupported: False
Sha1.IsSupported: True
Sha1.Arm64.IsSupported: True
Sha256.IsSupported: True
Sha256.Arm64.IsSupported: True
-- Single, Vector<Single>.Count=4 --
srcT: <-3.4028235E+38, Infinity, NaN, -1.2> # (FF7FFFFF 7F800000 FFC00000 BF99999A)
srcAllOnes: <NaN, NaN, NaN, NaN> # (FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF)
Abs(srcT): <3.4028235E+38, Infinity, NaN, 1.2> # (7F7FFFFF 7F800000 7FC00000 3F99999A)
發現用 .NET 5.0 運行時,部分ARM內在函數是不被支持的,例如 Dp、Rdm。
而之前查看 cpuinfo信息 時,Features里有 asimddp、asimdrdm,且剛纔用 .NET 7.0 測試時是支持的。看來硬體是支持的,只是 .NET 5.0 軟體不支持而已。
即:雖然 .NET 5.0 定義了ARM內在函數,但是在某些平臺上的.NET 5.0運行時,沒能支持所有的內在函數。直到 .NET 7.0,才全部解決了內在函數的支持性問題。
參考文獻
- Microsoft《在 Ubuntu 上安裝 .NET SDK 或 .NET 運行時》. https://learn.microsoft.com/zh-cn/dotnet/core/install/linux-ubuntu
- Microsoft《通過 Snap 安裝 .NET SDK 或 .NET Runtime》. https://learn.microsoft.com/zh-cn/dotnet/core/install/linux-snap
- Microsoft《使用安裝腳本或通過提取二進位文件在 Linux 上安裝 .NET》. https://learn.microsoft.com/zh-cn/dotnet/core/install/linux-scripted-manual
- Microsoft《dotnet 命令》. https://learn.microsoft.com/zh-cn/dotnet/core/tools/dotnet
- zyl910《在亞馬遜AWS上創建ARM伺服器並配置好RDP遠程桌面連接的心得》. https://www.cnblogs.com/zyl910/p/aws_create_arm_server_config_rdp.html
- zyl910《.NET 向量類型的運算結果範例——用於學習Vector類所提供百多個向量方法》. https://www.cnblogs.com/zyl910/p/dotnet_simd_VectorClassDemo.html