3

I am trying to use the WinSCP within my C# project for some ftp automation. I am using Visual Studio 2012 and am unsure on how to get it to work.

Things I have done so far:

  • I copied the WinSCP.exe into my project folder.
  • I imported a reference to the WinSCPnet.dll into my project
  • I ran C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe Path/To/WINSCPnet.DLL /codebase /tlb

This is my code as of now:

using WinSCP;

static void Main(string[] args)
{
    try
    {
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Ftp,
            HostName = "hostname.address.com",
            UserName = "Username",
            Password = "Password"
        };
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e);

    }
}

When I run this in code I get the following exception:

Could not load file or assembly 'WinSCP, Version=1.0.7.3446, Culture=neutral,
PublicKeyToken=b5f19f5762436b89' or one of its dependencies. The module was expected to
contain an assembly manifest.

And Visual Studio's error is: BadImageFormatException was unhandled.

I am not exactly sure how to import WinSCP into my C# program and would appreciate some guidance here.

Conclusion I was using 5.2.5 Beta and 32bit but my project was 64 bit

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ishikawa
  • 658
  • 1
  • 6
  • 14

1 Answers1

4

I suppose your project is 64-bit, while the distributed binary of WinSCP .NET assembly is only 32-bit. Either switch your project to 32-bit or build 64-bit version of the assembly from source code.

Also note that you do not need to register the assembly (regasm). That's for COM interop only. While you use it directly.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • My project was 64 bit, once I changed it to 32 bit it worked. Thank you so much for taking the time to help! – Ishikawa Nov 10 '13 at 22:42