SFTP Server code examples, where?

0
I want to use your SFTP Server but your code examples seem very sparse to non-existent. I don't need Windows Forms or WPF examples, simple console examples will do. Among other things I would like to see examples of using asynchronous events for monitoring the server operations. Also documentation with suggestions for configuring the server would be nice.
 
asked 5/7/2019 3:50:47 PM
add a comment

1 Answers

0
Please have a look at this one: using System; using ComponentPro.Net.Servers; using ComponentPro.Net; using System.IO; namespace Saml2Demo { class Program { static void Main() { ComponentPro.Licensing.Common.LicenseManager.SetLicenseKey("{TrialLicenseKey}"); using (var _server = new SftpSshServer()) { // Add a private key var rsaKey = GenerateOrGetServerKey(); _server.HostKeyStorage.Add(rsaKey); string rootDir = AppDomain.CurrentDomain.BaseDirectory "root"; Directory.CreateDirectory(rootDir); AddUser(_server, "test", "test", Path.GetFullPath(rootDir)); StartServer(_server, 2222, true); while (true) { System.Threading.Thread.Sleep(1000); } } } static int _currentPort = -1; /// /// Starts or stops the server. /// /// true to start the server; otherwise false to stop the server. static void StartServer(SftpSshServer server, int port, bool start) { if (start) { bool bind = false; if (_currentPort == -1) // Start the server for the first time? { _currentPort = port; bind = true; } else if (port != _currentPort) // If the server port has changed, we need to unbind the old port and bind to the new one. { // Unbind the old port. server.ClosePort(_currentPort); _currentPort = port; bind = true; } if (bind) { // Enable SFTP subsystem server.OpenPort(_currentPort, FileServerProtocol.Sftp); // Enable shell-like SCP subsystem server.OpenPort(_currentPort, FileServerProtocol.Scp); } // Start the server server.Start(); Console.WriteLine("SFTP and SCP file server is running. Port: {0}.", port); } else { // Stop the server server.Stop(); Console.WriteLine("Server has been stopped."); } } static SecureShellPrivateKey GenerateOrGetServerKey() { SecureShellPrivateKey rsaKey; string keyFile = AppDomain.CurrentDomain.BaseDirectory "serverkey.key"; const string keyFilePassword = "pass"; if (!File.Exists(keyFile)) { // Generate a private key for the server if the key file does not exist. rsaKey = SecureShellPrivateKey.Create(SecureShellHostKeyAlgorithm.RSA, 1024); rsaKey.Save(keyFile, keyFilePassword, SecureShellPrivateKeyFormat.Pkcs8); } else { // Load the key file if it exists. rsaKey = new SecureShellPrivateKey(keyFile, keyFilePassword); } return rsaKey; } static void AddUser(SftpSshServer server, string username, string password, string path) { // Create a new user with the new virtual root path FileServerUser user = new FileServerUser(username, password, path); bool addToList = true; // If a user with the same username already exists, remove that one. if (server.Users.Contains(username)) { server.Users.Remove(username); addToList = false; } // Add the new user. server.Users.Add(user); } } }
 
answered 5/8/2019 4:32:35 PM
add a comment

Your Answer

Not the answer you're looking for? Browse other questions tagged sftp server or ask your own question.