MrC - 2008-01-15

I've been using your code to download some files from a server and all was going well until I tried to call the Close method on the SFTP object. When I do it throws a Null Reference Exception. I hope there is a simple solution. I'm connecting to freeFTPd (Which also has SFTP) 1.0.11 if that is relevant?

Below is my code:-

            Sftp oSFTP = new Sftp(Host, Username, Password);
            try
            {
                //SFTP to get files               
                oSFTP.Connect();
                ArrayList theFiles = oSFTP.GetFileList(RemoteDir);
                foreach (string theFile in theFiles)
                {
                    if (theFile != "." && theFile != "..")
                    {
                        Console.WriteLine("Start: " + theFile);
                        oSFTP.Get(theFile, string.Format(@"{0}\{1}", LocalDir, Path.GetFileName(theFile)));
                        Console.WriteLine("Done: " + theFile);
                        oSFTP.Rm(theFile);
                        Console.WriteLine("Removed: " + theFile);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            oSFTP.Close();

Exception Details:-

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="Tamir.SharpSSH"
  StackTrace:
       at Tamir.SharpSsh.jsch.Session.disconnect()
       at Tamir.SharpSsh.SshBase.Close()
       at MySCP.Program.Main(String[] args) in E:\Sandboxes\MySCP\Program.cs:line 59
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

P.S. How do I find out if a file is a file or directory?