private void sessionControl_LaunchSession(object sender, LaunchSessionEventArgs se) { if (se != null) { if (se.program == LaunchSessionEventArgs.PROGRAM.PUTTY) { String errMsg = sc.launchSession(se.SessionName()); if (errMsg.Equals("") == false) { MessageBox.Show("PuTTY Failed to start.\nCheck the PuTTY location in System Tray -> Options.\n" + errMsg , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (se.program == LaunchSessionEventArgs.PROGRAM.PSFTP) { String errMsg = sc.launchPSFTP(se.SessionName()); if (errMsg.Equals("") == false) { MessageBox.Show("PSFTP Failed to start.\nCheck the PSFTP location in System Tray -> Options.\n" + errMsg , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (se.program == LaunchSessionEventArgs.PROGRAM.FILEZILLA) { String errMsg = sc.launchOtherSession(se.session, se.program); if (errMsg.Equals("") == false) { MessageBox.Show("FileZilla Failed to start.\nCheck the FileZilla location in System Tray -> Options.\n" + errMsg , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (se.program == LaunchSessionEventArgs.PROGRAM.WINSCP) { String errMsg = sc.launchOtherSession(se.session, se.program); if (errMsg.Equals("") == false) { MessageBox.Show("WinSCP Failed to start.\nCheck the WinSCP location in System Tray -> Options.\n" + errMsg , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
/// <summary> /// Lauch a WinSCP or FileZilla session /// </summary> /// <param name="s">The session to launch</param> /// <returns>The error message if the process fails to start</returns> public string launchOtherSession(Session s, LaunchSessionEventArgs.PROGRAM program) { String errMsg = ""; // Get all the values from the registry that we are interested in string hostname = s.Hostname; string username = s.Username; string protocol = s.Protocol; int portnumber = s.Portnumber; // Override the default username if stored in the hostname if (hostname != null && hostname.Contains("@")) { username = hostname.Substring(0, hostname.IndexOf("@")); if (hostname.IndexOf("@") < hostname.Length) hostname = hostname.Substring(hostname.IndexOf("@") + 1); } String execLocation = ""; String execArgs = ""; // Setup the FileZilla args if (program == LaunchSessionEventArgs.PROGRAM.FILEZILLA) { // Only bother if we have a hostname set if (hostname == null || hostname.Length == 0) { execArgs = ""; } else { // Setup the protocol and port Protocol fp = (Protocol)Properties.Settings.Default.FileZillaProtocol; switch (fp) { case Protocol.FTP: protocol = "ftp://"; portnumber = 21; break; case Protocol.FTPS: protocol = "ftps://"; portnumber = 990; break; case Protocol.SFTP: protocol = "sftp://"; portnumber = 22; break; case Protocol.AUTO: if (protocol.Equals("ssh", StringComparison.InvariantCultureIgnoreCase)) { protocol = "sftp://"; if (portnumber == -1) portnumber = 22; } else { protocol = "ftp://"; portnumber = 21; } break; } String password = ""; if (Properties.Settings.Default.FileZillaVersion == 2) { // Setup Pageaent auth if requested and the protocol is sftp if (protocol.Equals("sftp://") && Properties.Settings.Default.FileZillaAttemptKeyAuth == true) { password = "******"; } } else if (Properties.Settings.Default.FileZillaVersion == 3) { execArgs = "-l interactive "; } // Finalise the auth string String auth = ""; if (username != null && !(username.Equals(""))) auth = username + password + "@"; execArgs = execArgs + protocol + auth + hostname + ":" + portnumber; } execLocation = Properties.Settings.Default.FileZillaLocation; } else if (program == LaunchSessionEventArgs.PROGRAM.WINSCP) { // Setup the /ini option if (Properties.Settings.Default.WinSCPIniEnabled == true && Properties.Settings.Default.WinSCPIniLocation != null && !Properties.Settings.Default.WinSCPIniLocation.Equals("") ) { execArgs = "/ini=\"" + Properties.Settings.Default.WinSCPIniLocation + "\" "; } // Setup the /privatekey option if ( !(s.PrivateKeyLocation.Equals("")) ) { execArgs = "/privatekey=\"" + s.PrivateKeyLocation + "\" "; } // Only bother if we have a hostname set if (hostname != null && hostname.Length != 0) { // Setup the protocol and port Protocol wp = (Protocol)Properties.Settings.Default.WinSCPProtocol; int wsVer = Properties.Settings.Default.WinSCPVersion; // FTP isn't supported for v3, so default to SFTP if (wsVer == 3 && wp == Protocol.FTP) wp = Protocol.SFTP; switch (wp) { case Protocol.FTP: protocol = "ftp://"; portnumber = 21; break; case Protocol.SFTP: protocol = "sftp://"; portnumber = 22; break; case Protocol.SCP: protocol = "scp://"; portnumber = 22; break; case Protocol.AUTO: if (protocol.Equals("ssh", StringComparison.InvariantCultureIgnoreCase)) { Protocol wpp = (Protocol)Properties.Settings.Default.WinSCPPrefProtocol; if (wpp == Protocol.SCP) { protocol = "scp://"; } else { protocol = "sftp://"; } if (portnumber == -1) portnumber = 22; } else if (wsVer == 4) { protocol = "ftp://"; portnumber = 21; } else { protocol = "sftp://"; portnumber = 22; } break; } // Finalise the auth string String auth = ""; if (username != null && !(username.Equals(""))) auth = username + "@"; execArgs = execArgs + protocol + auth + hostname + ":" + portnumber; } execLocation = Properties.Settings.Default.WinSCPLocation; } Process p = new Process(); p.StartInfo.FileName = execLocation; p.StartInfo.Arguments = execArgs; // Attempt to start the process try { p.Start(); } catch (Exception ex) { errMsg = ex.Message; } p.Close(); return errMsg; }
protected virtual void OnLaunchSession(LaunchSessionEventArgs se) { if (LaunchSession != null) { // Hide the form if the option has been requested if (Properties.Settings.Default.MinimizeOnUse == true && ParentForm.Visible) ParentForm.Hide(); LaunchSession(this, se); } }