Loading WinSCP in PowerShell - Could not load file

2020-08-01 06:10发布

问题:

Error Message:

Add-Type : Could not load file or assembly 'file:///D:\WinSCP-5.15.2-Automation\WinSCPnet.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. I placed WinSCP.dll same folder together with WinSCP.exe**

I run PowerShell v2.0.

The problem exists in the error message

The script line that causes the problem looks as following:

#Load WinSCP .NET assembly
Add-Type -Path "C:\users\Desktop\WinSCPnet.dll"

This post almost similar with this link PowerShell runtime exception - "could not load file or assembly" and I tried every method posted in the link and unable to solve it. Is there anything I missed out?

回答1:

You probably have an old version of .NET Framework.

Recent versions of WinSCP .NET assembly target .NET Framework 4.0.

WinSCP 5.13.9 was the last version that worked in .NET 2.

You should use the latest version of WinSCP though. It's a security software after all. If you cannot install .NET Framework 4, you can use WinSCP scripting instead of .NET assembly. WinSCP itself does not have any dependency on .NET Framework (or anything else). And runs on all versions of Windows since Windows XP.