title: C# and Powershell, Download File From FTP
body: <p>I want to download the contents of a folder on an ftp server.
  With this Powershell script I did it:</p>
  
  <pre><code>#FTP Server Information - SET VARIABLES
      $ftp = "ftp://my.server.ftp" 
      $user = 'ftp_user' 
      $pass = 'password'
      $folder = '\folder\folder1\'
      $target = "C:\temp\"
  
      #SET CREDENTIALS
      $credentials = new-object System.Net.NetworkCredential($user, $pass)
  
      function Get-FtpDir ($url,$credentials) {
          $request = [Net.WebRequest]::Create($url)
          $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
          if ($credentials) { $request.Credentials = $credentials }
          $response = $request.GetResponse()
          $reader = New-Object IO.StreamReader $response.GetResponseStream() 
          $reader.ReadToEnd()
          $reader.Close()
          $response.Close()
      }
  
      #SET FOLDER PATH
      $folderPath= $ftp + "/" + $folder + "/"
  
      $Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
      $files = ($Allfiles -split "`r`n")
  
      $files 
  
      $webclient = New-Object System.Net.WebClient 
      $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
      $counter = 0
      foreach ($file in ($files | where {$_ -like "*.*"})){
          $source=$folderPath + $file  
          $destination = $target + $file 
          $webclient.DownloadFile($source, $target+$file)
  
          #PRINT FILE NAME AND COUNTER
          $counter++
          $counter
          $source
      }
  }
  </code></pre>
  
  <p>But now I'm trying to insert this powershell script inside a C # application.
  I have come this far:</p>
  
  <pre><code>static void Main(string[] args)
          {                       
              string ftp = "ftp://my.server.ftp";
              string user = "ftp_user";
              string pass = "password";
              string folder = "\\folder\\folder1\\";
              string target = "C:\\temp\\";
  
              NetworkCredential credentials = new NetworkCredential(user, pass);
  
              string folderPath = ftp + "/" + folder + "/";
  
              var AllFiles = GetDir(folderPath, credentials);
              ??? $files = ($Allfiles -split "`r`n") ???
          }
  
          public static Object GetDir(string url, NetworkCredential credentials)
          {
              WebRequest request = WebRequest.Create(url);
              request.Method = WebRequestMethods.Ftp.ListDirectory;
              request.Credentials = credentials;
              WebResponse response = request.GetResponse();
              StreamReader reader = new StreamReader(response.GetResponseStream());
              reader.ReadToEnd();
              reader.Close();
              response.Close();           
          } 
  </code></pre>
  
  <p>In the powershell script the "Get-FtpDir" function I don't know what object it returns and therefore I don't know how to translate these two statements:</p>
  
  <pre><code>$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
  $files = ($Allfiles -split "`r`n")
  </code></pre>
  
  <p>Can someone help me?</p>
  
  <p>Thanks in advance</p>