[.NET][筆記] - SshNet.Security.Cryptography - 系統找不到指定的檔案
起因
最近公司要寫一支程式自動上傳檔案到sftp,所以從NuGet下載了「SSH.NET(Renci.SshNet)」這個套件來使用。結果在測試時一直跳Exception,說系統找不到套件「SshNet.Security.Cryptography」的指定檔案:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMOv7NA_o5KgwI8DpweNFjygy6JDE_w599V_la6_K-aeA3UIVt7ne-C1IzuE81pR7pApMW1nMpJjJUY4WcPNMlUmYADjIhjb_BdbwB51TktVhlnOStPoWn4zA6t21pM5xZalXX0_-y4UTkz-45ukw8I4VzvXBm10SpCHVarS8zOtEkVHmqFQtY7HxPNqAh/s1600/dotnet_dll_not_found.png)
奇怪的是編譯能夠通過,而且輸出欄位也沒有給出錯誤。
原因
後來查來查去,發現是因為專案使用的.NET Framework版本是4.6.1,而
Renci.SshNet套件的.NET版本支援度是:
- .NET 6.0
- .NET 7.0
- .NET Standard 2.0
- .NET Standard 2.1
- .NET Framework 4.6.2
SshNet.Security.Cryptography套件的.NET版本支援度是:
所以在專案.NET Framework版本4.6.1的情況下,Visual Studio會自動參考.NET Standard 2.0版本的SSH.NET及.NET Framework 4.5版本的SshNet.Security.Cryptography。- .NET Standard 1.0
- .NET Standard 1.3
- .NET Standard 2.0
- .NET Framework 2.0
- .NET Framework 4.0
- .NET Framework 4.5
結果因為兩個套件的目標Framework版本不一樣,所以就掛掉了。
解決方法
解決方法就是從csproj手動更改SshNet.Security.Cryptography套件的目標package版本,改為使用.NET Standard 2.0就可以了。用Notepad++或Visual Studio Code等等自己用的順手的文字編輯器,打開專案的.csproj檔
找到以下內容:
<Reference Include="SshNet.Security.Cryptography, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\packages\SshNet.Security.Cryptography.1.3.0\lib\net45\SshNet.Security.Cryptography.dll</HintPath> </Reference>將
<HintPath>
中的路徑從net45
改為netstandard2.0
:<Reference Include="SshNet.Security.Cryptography, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\packages\SshNet.Security.Cryptography.1.3.0\lib\netstandard2.0\SshNet.Security.Cryptography.dll</HintPath> </Reference>之後再重建專案就OK了。
留言
張貼留言