コード例 #1
0
        // there is no built in way to delete a file using this utility, so we have to hack a bit...
        // info found here:  http://revdoniv.wordpress.com/2010/09/11/how-to-delete-an-sftp-file-in-net/
        internal void DeleteFile(string fileName)
        {
            var filePath = Path.Combine(_defaultDirectory, fileName);

            try
            {
                var session = new JSch().getSession(Properties.Settings.Default.SFTPUser, Properties.Settings.Default.SFTPHost, Properties.Settings.Default.SFTPPort);

                // you would think this would work, but nope... it doesnt...
                //session.setPassword(Properties.Settings.Default.SFTPPass);

                // you have to override this silly class
                session.setUserInfo(new SFTPUserInfo(Properties.Settings.Default.SFTPPass));

                session.connect();
                var sftpChannel = (ChannelSftp)session.openChannel("sftp");
                sftpChannel.connect();

                sftpChannel.rm(filePath);

                sftpChannel.disconnect();
                session.disconnect();
            }
            catch (Exception ex)
            {
                GlobalContext.Log("Unable to delete file from SFTP", true);
                GlobalContext.Log(ex.ToString(), true);
            }
        }