SshFileTransferTest.cs :  » Network-Clients » SharpSSH » sharpSshTest » sharpssh_samples » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SharpSSH 
SharpSSH » sharpSshTest » sharpssh_samples » SshFileTransferTest.cs
using System;
using System.Collections;
using Tamir.SharpSsh;

namespace sharpSshTest.sharpssh_samples{
  /// <summary>
  /// Sample showing the use of the SSH file trasfer features of 
  /// SharpSSH such as SFTP and SCP
  /// </summary>
  public class SshFileTransferTest
  {
    public static void RunExample()
    {    
      try
      {
        SshConnectionInfo input = Util.GetInput();
        string proto = GetProtocol();
        SshTransferProtocolBase sshCp;

        if(proto.Equals("scp"))
          sshCp = new Scp(input.Host, input.User);
        else
          sshCp = new Sftp(input.Host, input.User);

        if(input.Pass != null) sshCp.Password = input.Pass;
        if(input.IdentityFile != null) sshCp.AddIdentityFile( input.IdentityFile );
        sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
        sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
        sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);

        Console.Write("Connecting...");
        sshCp.Connect();
        Console.WriteLine("OK");

        while(true)
        {
          string direction = GetTransferDirection();
          if(direction.Equals("to"))
          {
            string lfile = GetArg("Enter local file ['Enter to cancel']");
            if(lfile=="") break;
            string rfile = GetArg("Enter remote file ['Enter to cancel']");
            if(rfile=="") break;
            sshCp.Put(lfile, rfile);
          }
          else
          {
            string rfile = GetArg("Enter remote file ['Enter to cancel']");
            if(rfile=="") break;
            string lpath = GetArg("Enter local path ['Enter to cancel']");
            if(lpath=="") break;
            sshCp.Get(rfile, lpath);
          }
        }

        Console.Write("Disconnecting...");
        sshCp.Close();
        Console.WriteLine("OK");
      }
      catch(Exception e)
      {
        Console.WriteLine(e.Message);
      }
    }

    public static string GetProtocol()
    {
      string proto = "";
      while( true )
      {
        Console.Write("Enter SSH transfer protocol [SCP|SFTP]: ");
        proto = Console.ReadLine();
        if(proto.ToLower().Equals("")) break;
        if( proto.ToLower().Equals("scp") || proto.ToLower().Equals("sftp"))
          break;
        Console.Write("Bad input, ");
      }
      return proto;
    }

    public static string GetTransferDirection()
    {
      string dir = "";
      while( true )
      {
        Console.Write("Enter transfer direction [To|From]: ");
        dir = Console.ReadLine();
        if(dir.ToLower().Equals("")) break;
        if( dir.ToLower().Equals("to") || dir.ToLower().Equals("from"))
          break;
        Console.Write("Bad input, ");
      }
      return dir;
    }

    public static string GetArg(string msg)
    {
      Console.Write(msg+": ");
      return Console.ReadLine();
    }

    static ConsoleProgressBar progressBar;

    private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
    {
      Console.WriteLine();
      progressBar = new ConsoleProgressBar();
      progressBar.Update(transferredBytes, totalBytes, message);
    }

    private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
    {
      if(progressBar!=null)
      {
        progressBar.Update(transferredBytes, totalBytes, message);
      }
    }

    private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
    {
      if(progressBar!=null)
      {
        progressBar.Update(transferredBytes, totalBytes, message);
        progressBar=null;
      }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.