web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[Renci.SshNet.Sftp.SftpFile]' to type 'System.Collections.ArrayList'.

(0) ShareShare
ReportReport
Posted on by

Hi,

I am trying to download all files from SFTP using Renci.SSHNET library.

My code is as follows:

..

Renci.SshNet.SftpClient                     cliente;

System.Collections.ArrayList                list;

cliente = new Renci.SshNet.SftpClient(sftpHost,sftpPort,sftpUserName,sftpPassword);

cliente.Connect();

cliente.ChangeDirectory("outbound");

list = cliente.ListDirectory("/outbound/",null);

..

It works perfect when I run on client. But when I run on batch it throws the error below:  System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[Renci.SshNet.Sftp.SftpFile]'  to type 'System.Collections.ArrayList'.

Any ideas? how can I run this class on batch?

Thanks.

Svl.

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    It means that the method returns List<SftpFile> and you're trying to assign it to ArrayList, which fails because these types are not compatible. What you should do instead depends on what you're trying to achieve - please tell us what you want to do with the list.

  • Community Member Profile Picture
    on at

    Hi Martin, thanks for your reply.

    I want to get the number of files in the directory, then download the files with the specific prefixes. But it fails because of the incompatible types when i try to run on server.

    My method continues like that:

    ..

           fileCount = List.get_Count();

           for(i = 0;i < filecount; i++)

           {

               lists.empty();

               packedList = conNull();

               files = list.get_Item(i);

               strlist = files.ToString();

               lists=strSplit(strlist,',');

               iterator = new ListIterator(lists);

               if(iterator.more())

               {

                   packedList += iterator.value();

                   iterator.next();

               }

               prefix = subStr(conPeek(packedList,1),6,7);

               if(prefix == "SKA_"   || prefix == "SKS_")

               {

                   fileName        = subStr(conPeek(packedList,1),6,30);

                   targetFile      = System.IO.Path::Combine(myPath,fileName);

                   memStream       = new System.IO.FileStream(targetFile,System.IO.FileMode::Create);

                   asyncResult     = cliente.BeginDownloadFile(fileName, memStream);

                  cliente.EndDownloadFile(asyncResult);

                  memstream.Close();

                  cliente.DeleteFile(Filename);

               }

    }

    ..

  • Verified answer
    Martin Dráb Profile Picture
    239,040 Most Valuable Professional on at

    I'm not sure how your code is supposed to work. Anyway, it seems that you don't need anything more than to iterate the collection, therefore I would use the non-generic interface IEnumerable.

    Please look at this code; just bear in mind it's been written without any compiler or anything.

    System.Collections.IEnumerable list = cliente.ListDirectory("/outbound/", null);
    System.Collections.IEnumerator enumerator = list.GetEnumerator();
    Renci.SshNet.Sftp.SftpFile sftpFile;
    str name;
    
    while (enumerator.MoveNext())
    {
        sftpFile = enumerator.get_Current();
        name = sftpFile.get_Name();
    }
  • Community Member Profile Picture
    on at

    Thanks a million, Martin :)

    Problem solved.

    Have a nice day!

  • syed baber Profile Picture
    11,633 on at

    I had the same issues and creating object of type IEnumerable solved the issue for me.

    Thanks Martin for excellent answer.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Joris dG Profile Picture

Joris dG 5

#2
Andrew Jones a1x Profile Picture

Andrew Jones a1x 2

#3
GL-01081504-0 Profile Picture

GL-01081504-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans