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

Notifications

Announcements

No record found.

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
    237,801 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
    237,801 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans