title: Exception has been thrown by the target of an invocation (SSIS & WinSCP & C#)
body: <p>I'm getting this error message while trying to execute my SCRIPT TASK in SSIS (I'm using VS 2019 and C#).</p>
  <p>Been trying for a while it seems it wont work.</p>
  <blockquote>
  <p>Error message: Exception has been thrown by the target of an
  invocation</p>
  </blockquote>
  <p><strong>Details:</strong></p>
  <pre><code>   in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
     in System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
     in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
     in System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
     in Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
  </code></pre>
  <hr />
  <p><strong>This is my code:</strong></p>
  <pre><code>#region Namespaces
  using System;
  using System.Data;
  using Microsoft.SqlServer.Dts.Runtime;
  using System.Windows.Forms;
  using System.Linq;
  using WinSCP;
  #endregion
  
  namespace ST_2cfb1a0997cf7b332da4453v3g1132g
  {
      [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
      public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
      {
              public void Main()
              {
  
                      SessionOptions sessionOptions = new SessionOptions
                      {
                          Protocol = Protocol.Sftp,
                          HostName = &quot;xxxxxxxxxxxxxxxxxxxxxxxxx&quot;,
                          UserName = &quot;xxxxxxxxxxxxxxxxxxxxxxxxx&quot;,
                          Password = &quot;xxxxxxxxxxxxxxxxxxxxxxxxx&quot;,
                      };
  
                      using (Session session = new Session())
                      {
                          session.Open(sessionOptions);
  
                          const string externalPath = &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;;
                          const string PathLocal = @&quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;;
  
                          RemoteDirectoryInfo directoryInfo = session.ListDirectory(externalPath);
  
      
                          RemoteFileInfo latest =
                              directoryInfo.Files
                                  .Where(file =&gt; !file.IsDirectory)
                                  .OrderByDescending(file =&gt; file.LastWriteTime)
                                  .FirstOrDefault();
  
                          if (latest == null)
                          {
                              throw new Exception(&quot;Error, not found&quot;);
                          }
  
                          session.GetFiles(
                              externalPath.EscapeFileMask(latest.FullName), PathLocal).Check();
                      }
  
              }
  
          #region ScriptResults declaration
          /// &lt;summary&gt;
          /// This enum provides a convenient shorthand within the scope of this class for setting the
          /// result of the script.
          /// 
          /// This code was generated automatically.
          /// &lt;/summary&gt;
          enum ScriptResults
          {
              Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
              Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
          };
          #endregion
  
      }
  }
  </code></pre>
  <p>What is going on in here?</p>
  <p>Am i doing anything wrong?</p>