• WINSCP 使用笔记


    前期准备:

    1.官网下载:http://winscp.net/eng/docs/lang:chs

    官网C#示例:http://winscp.net/eng/docs/library#csharp

    当然还有很多,自行选择

    2.准备FTP站点

    代码:

      private bool moveFile(Protocol FtpType, string HostName, string UserName, string Password,int PortNumber,string SshHostKeyFingerprint,string Path)
            {
                try
                {
                    // Setup session options
                    SessionOptions sessionOptions = new SessionOptions();
                    sessionOptions.Protocol = FtpType;
                    sessionOptions.HostName = HostName;
                    sessionOptions.UserName = UserName;
                    sessionOptions.Password = Password;
                    sessionOptions.PortNumber = PortNumber;
                    if (FtpType == Protocol.Sftp)
                        sessionOptions.SshHostKeyFingerprint = "SshHostKeyFingerprint";
    
                    using (Session session = new Session())
                    {
                        // Connect
                        session.Open(sessionOptions);
    
                        // Upload files
                        TransferOptions transferOptions = new TransferOptions();
                        transferOptions.TransferMode = TransferMode.Binary;
    
                        TransferOperationResult transferResult;
                        string sourcePath = Temp["FilePath"].ToString();
                        if (!File.Exists(sourcePath)) { RBAppText("警告:文件不存在,已跳过"); return false; }
                        string fileName = System.IO.Path.GetFileNameWithoutExtension(sourcePath);
                        string extension = System.IO.Path.GetExtension(sourcePath);
    
                        string remotePath = "/" + Path + "/" + fileName + DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
                        transferResult = session.PutFiles(sourcePath, remotePath, true, transferOptions);
    
                        // Throw on any error
                        transferResult.Check();
    
                        //Print results
                        if (transferResult.Transfers.Count < 1) { RBAppText("警告:没有文件需要处理"); }
                        else
                        {
                            foreach (TransferEventArgs transfer in transferResult.Transfers)
                            {
                                RBAppText("提示:文件处理完成:" + transfer.FileName);
                            }
                        }
                    }
                    return true;
                }
                catch (Exception e)
                {
                    RBAppText("错误:" + e.Message);
                    return false;
                }
            }
    作者:Joe.Fan
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Set Matrix Zeroes
    Summary Ranges
    Triangle
    MongoDB基本使用
    PHP Liunx 服务安全防范方案
    linux lvs 配置
    Linux下cacti的安装
    linux_nmon监控方法
    linux下rsync命令详细整理
    虚拟机安装CentOS以及SecureCRT设置【完美无错版】
  • 原文地址:https://www.cnblogs.com/fanxingthink/p/5728151.html
Copyright © 2020-2023  润新知