.NET Core 筆記

使用 vscode

  • dotnet 指令

    • 產生sln檔案
      ​​​​​​mkdir project
      ​​​​​​cd project
      ​​​​​​dotnet new sln
      
    • 建立類別函式庫專案
      ​​​​​​dotnet new classlib -o [classlib name]
      ​​​​​​dotnet new classlib -o Utils
      
    • 建立app專案
      ​​​​​​dotnet new console -o [app name]
      ​​​​​​dotnet new console -o testApp
      
    • 新增專案到sln檔中
      ​​​​​​dotnet sln add Utils/Utils.csproj
      ​​​​​​dotnet sln add testApp/testApp.csproj
      
    • 新增專案參考
      ​​​​​​dotnet add testApp/testApp.csproj reference Utils/Utils.csproj
      
  • nuget 相關

    • 建立 nuget.config
      ​​​​​​dotnet new nugetconfig
      
    • 查詢目前本機端的 source
      ​​​​​​dotnet nuget locals all --list
      
    • 調整預設的global source 到指定位置.
      .net6之後, package不會預設出現在專案下, 預設會出現在下列位置
      ​​​global-packages: C:\Users\ivan\.nuget\packages\
      
      其目的是為了避免多專案都使用到相同的package而造成空間的浪費.
      可透過調整nuget.config改存放到指定位置.
      ​​​​​​<config>
      ​​​​​​   <add key="globalPackagesFolder" value="./packages" />
      ​​​​​​   <add key="repositoryPath" value="./packages" />
      ​​​​​​</config>
      ​​​​​​<packageRestore>
      ​​​​​​   <add key="enabled" value="false" />
      ​​​​​​   <add key="automatic" value="false" />
      ​​​​​​</packageRestore>
      
  • 如何使用單一檔案部署

    • 調整 csproj 檔案
      加上下列幾行
      ​​<PropertyGroup>
      ​​    <OutputType>Exe</OutputType>
      ​​    <TargetFramework>net6.0</TargetFramework>
      ​​    <ImplicitUsings>enable</ImplicitUsings>
      ​​    <Nullable>enable</Nullable>
      ​​    <PublishSingleFile>true</PublishSingleFile>
      ​​    <SelfContained>true</SelfContained>
      ​​    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
      ​​    <PublishReadyToRun>true</PublishReadyToRun>
      ​​</PropertyGroup>
      
      • SelfContained : publish 時是否使用單一檔案部署.
      • RuntimeIdentifier : 執行平台, 也可於publish時使用 -r 來表示.
    • 調整exe運作環境
      • 修改 RuntimeIdentifier
      • 使用指令發佈成linux-x64平台
        ​​​​​​​​​​​​dotnet publish -r linux-x64
        
      • 發佈平台參考
  • 參考網頁
    微軟教學網頁
    .net 6 console template 變動
    單一檔案部屬

如何讀取設定檔