title: Uploading to an FTP server
body: <p>I am working on a tiny tiny application that just uploads a file to the FTP server. I have reviewed my code, but I am quite unable to locate the problem. Here is the code,</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &quot;stdafx.h&quot;
  
  using namespace System;
  
  #include &quot;iostream&quot;
  #include &lt;conio.h&gt;
  
  using namespace System;
  
  void UploadFiles(String ^_FileName, String ^_UploadPath, String ^_FTPUser, String ^_FTPPass);
  
  int main ()
  {
      // Upload file using FTP
      UploadFiles(&quot;c:\\test.html&quot;, &quot;ftp://playbabe.tk/public_html/test.html&quot;, &quot;xxxxxx&quot;, &quot;xxxxxx&quot;);
      return 0;
  }
  
  void UploadFiles(System::String ^_FileName, System::String ^_UploadPath, System::String ^_FTPUser, System::String ^_FTPPass)
  {
      System::IO::FileInfo ^_FileInfo = gcnew System::IO::FileInfo(_FileName);
  
      // Create FtpWebRequest object from the Uri provided
      System::Net::FtpWebRequest ^_FtpWebRequest = safe_cast&lt;System::Net::FtpWebRequest^&gt;(System::Net::FtpWebRequest::Create(gcnew Uri(_UploadPath)));
  
      // Provide the WebPermission Credintials
      _FtpWebRequest-&gt;Credentials = gcnew System::Net::NetworkCredential(_FTPUser, _FTPPass);
  
      // By default KeepAlive is true, where the control connection is not closed
      // after a command is executed.
      _FtpWebRequest-&gt;KeepAlive = false;
  
      // set timeout for 20 seconds
      _FtpWebRequest-&gt;Timeout = 20000;
  
      // Specify the command to be executed.
  
      _FtpWebRequest-&gt;Method =System::Net::WebRequestMethods::Ftp.UploadFile;
  
      // Specify the data transfer type.
      _FtpWebRequest-&gt;UseBinary = true;
  
      // Notify the server about the size of the uploaded file
      _FtpWebRequest-&gt;ContentLength = _FileInfo-&gt;Length;
  
      // The buffer size is set to 2kb
      int buffLength = 2048;
      array&lt;System::Byte&gt; ^buff = gcnew array&lt;System::Byte&gt;(buffLength);
  
      // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
      System::IO::FileStream ^_FileStream = _FileInfo-&gt;OpenRead();
  
      try
      {
          // Stream to which the file to be upload is written
          System::IO::Stream ^_Stream = _FtpWebRequest-&gt;GetRequestStream();
  
          // Read from the file stream 2kb at a time
          int contentLen = _FileStream-&gt;Read(buff, 0, buffLength);
  
          // Till Stream content ends
          while (contentLen != 0)
          {
              // Write Content from the file stream to the FTP Upload Stream
              _Stream-&gt;Write(buff, 0, contentLen);
              contentLen = _FileStream-&gt;Read(buff, 0, buffLength);
          }
  
          // Close the file stream and the Request Stream
          _Stream-&gt;Close();
          delete _Stream;
          _FileStream-&gt;Close();
          delete _FileStream;
      }
      catch (Exception ^ex)
      {
          //MessageBox::Show(ex-&gt;Message, &quot;Upload Error&quot;, MessageBoxButtons::OK, MessageBoxIcon::Error);
          std::cout&lt;&lt;&quot;error&quot;;
      }
  
      getch();
  }
  </code></pre>
  <p>It gives two errors in Visual C++ 2010 Express,</p>
  <blockquote>
  <p>Error   1   error C2275: <code>'System::Net::WebRequestMethods::Ftp'</code> : illegal use of this type as an expression  C:\Users\Me\documents\visual studio 2010\Projects\test1\test1\test1.cpp 70</p>
  <p>Error   2   error C2228: left of <code>'.UploadFile'</code> must have class/struct/union C:\Users\Me\documents\visual studio 2010\Projects\test1\test1\test1.cpp 70</p>
  </blockquote>
  <p>I am not sure what is going wrong here.</p>