Contact Info

Crumbtrail

ActiveXperts.com » Network Component » How to Use Network Component » SFTP » Visual CSharp.NET

Visual C# .NET SFTP Sample Source Code

Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.

Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.

Network Component can be well integrated into any development platform that supports ActiveX objects.


The most important functions of the SFtp object are:


Step 1: Download and install the Network Component

Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.

Step 2: Create a new Visual C# .NET Project

Launch Microsoft Visual Studio from the Start menu. Choose 'New' from the 'File' menu and click on 'Web Site'. In the 'Web Site' dialog, select ASP .NET Web Site. Select a name for the application and a name for the solution. Also, select the directory where you want to store the project:

ASP.NET

(Click on the picture to enlarge)

Step 3: Refer to the Network Component Library and create the objects

Now that a new project has been created, you must add a reference to the Network Component library in the project to be able to use the Network Component object. To do so, choose 'Add Reference...' from the 'Project' menu. In the 'Add Reference' dialog that pops up, select the 'COM' tab and select the 'Network Component Type Library' as shown in the following picture:

ASP.NET

(Click on the picture to enlarge)

Click 'OK' to close the 'Add Reference' dialog.

On top of your code, type the following line to use the Network Component namespace:

using AxNetwork;

In your Main function, declare and create the following object:

public SFTP objSftp;
objSftp = new SFTP();

Appendix: Full source code

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using AxNetwork;

namespace SFtpGetDemo
{
  class SFtpGetDemo
  {
    static void Main(string[] args)
    {
      AxNetwork.SFtp objSFtp = new AxNetwork.SFtp();
      AxNetwork.SFtpFile objSFtpFile = null;

      // A license key is required to unlock this component after the trial period has expired.
      // Call 'Activate' with a valid license key as its first parameter. Second parameter determines whether to save the license key permanently 
      // to the registry (True, so you need to call Activate only once), or not to store the key permanently (False, so you need to call Activate
      // every time the component is created). For details, see manual, chapter "Product Activation".
      /*
      objSFtp.Activate( "XXXXX-XXXXX-XXXXX", false );
      */

      // Display component information
      Console.WriteLine("ActiveXperts Network Component {0}\nBuild: {1}\nModule: {2}\nLicense Status: {3}\nLicense Key: {4}\n", objSFtp.Version, objSFtp.Build, objSFtp.Module, objSFtp.LicenseStatus, objSFtp.LicenseKey);

      // Set Logfile (optional, for debugging purposes)
      objSFtp.LogFile = Path.GetTempPath() + "Sftp.log";
      Console.WriteLine("Log file used: {0}\n", objSFtp.LogFile);

      // Connect
      objSFtp.Host = "srv202.activexperts-labs.com";
      objSFtp.Port = 22;
      objSFtp.UserName = "activexperts";
      objSFtp.Password = "topsecret";
      objSFtp.AcceptHostKey = true;        // Automatically accept and store the host key if it is changed or unknown
      //  objSftp.PrivateKeyFile = "C:\\keyfile.ppk";  // a private key file can be used instead of a password
      objSFtp.Connect();            
      Console.WriteLine("Connect, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")");
      if (objSFtp.ProtocolError != "")
        Console.WriteLine("ProtocolError: " + objSFtp.ProtocolError);
      if (objSFtp.LastError == 0)
      {

        // Change directory
        objSFtp.ChangeDir("/network-component");
        Console.WriteLine("ChangeDir, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")");
        if (objSFtp.LastError == 0)
        {
          // Iterate over all files
          objSFtpFile = (SFtpFile)objSFtp.FindFirstFile(".");
          Console.WriteLine("FindFirstFile, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")");
          while (objSFtp.LastError == 0)
          {
            Console.WriteLine("Name: " + objSFtpFile.Name);
            Console.WriteLine("   IsDirectory: " + objSFtpFile.IsDirectory);
            Console.WriteLine("   Size (bytes): " + objSFtpFile.SizeBytes);
            Console.WriteLine("   Creation date (seconds): " + objSFtpFile.DateSeconds);
            Console.WriteLine("   Creation date: " + objSFtpFile.Date);

            /*if (objSFtpFile.IsDirectory == 0) {
              // To save the file, call the GetFile function. 
              strSaveAs = "C:\\temp\\" + objSFtpFile.Name;
              objSFtp.GetFile( objSFtpFile.Name, strSaveAs ) ;                        
              Console.WriteLine( "GetFile, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")" );

              // To delete a file, call the DeleteFile function. 
              objSFtp.DeleteFile(objSFtpFile.Name);
              Console.WriteLine( "DeleteFile, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")" );
            } */

            objSFtpFile = (SFtpFile)objSFtp.FindNextFile();
            Console.WriteLine("FindNextFile, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")");
          }
        }

        objSFtp.Disconnect();
        Console.WriteLine("Disconnect, result: " + objSFtp.LastError.ToString() + " (" + objSFtp.GetErrorDescription(objSFtp.LastError) + ")");
    }

      Console.WriteLine("Ready.");
      System.Threading.Thread.Sleep(5000);	// Sleep for 5 seconds before exit		
    }
  }
}

You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.

NOTE: Demo Projects are created with Microsoft Visual Studio 2008

The Network Component project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2008.

Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.