Example #1
0
        public static bool moveSecureFile(Uri serverUri, string filename, string username, string password, bool UseSSL, string destination)
        {
            // The serverUri parameter should use the ftp:// scheme.
            // It contains the name of the server file that is to be deleted.
            // Example: ftp://contoso.com/someFile.txt.
            //
            if (serverUri.Scheme != Uri.UriSchemeFtp) {
                return false;
            }

            Sftp sftp = new Sftp(serverUri.Host, username, password);
            if (sftp == null) {
                //Error connecting to FTP server
                throw new WebException("ERROR - " + ClassName + ": Error connecting to FTP server", WebExceptionStatus.SecureChannelFailure);
            } else {

                try {
                    //Ensure the folder separator exists
                    if (!destination.EndsWith("/")) {
                        destination += "/";
                    }

                    sftp.Connect();
                    sftp.Rename(serverUri.LocalPath + filename, Path.Combine(serverUri.LocalPath, destination + filename));

                    return true;

                } catch (Tamir.SharpSsh.jsch.SftpException sfex) {
                    // write to log - note the lowercase m in message - this library uses nonstandard error reporting
                    Trace.TraceInformation("ERROR - " + ClassName + ":" + sfex.message);
                    throw new WebException("ERROR - " + ClassName + ": " + sfex.message);

                } catch (Exception ex) {
                    // write to log
                    Trace.TraceInformation("ERROR - " + ClassName + ":" + ex.Message);
                    //EventLog.WriteEntry(Application.ProductName, "ERROR - " + ClassName + ":" + ex.Message, EventLogEntryType.Error);
                    throw new WebException("ERROR - " + ClassName + ": " + ex.Message);
                    //return false;

                } finally {
                    sftp.Close();
                }
            }
        }