.net 的SSH操作linux文件

我们知道在开发一个网站的时候,有时候会有多个服务器,一个应用服务器和数据服务器,而且文件服务器一般都是linux系统,那么,今天的问题就来了,就是怎么跨服务器操作linux系统的文件,比如我们要在linux上面做上传文件,下载文件,删除文件等操作呢?下面开始我们的主题。

不知道大家有没有听过Rechi.SSHNet类库,这个类库是开源的,用SFTP协议操作文件,它是基于协议。

using System.Collections.Generic;   
using System.Text;  
using System;  
using System.Collections;  
using System.IO;  
using Renci.SshNet;  
namespace SFTPHelper  
{  
   
        /************************描述 SFTP操作类****************************************** 
        **创建者  : aaa 
        **创建时间: 2017-03-11 
        **描述    : SFTP操作类 
        *********************************************************************************/  
  
        /// <summary>  
        /// SFTP操作类  
        /// </summary>  
        public class SFTPOperation  
        {  
            #region 字段或属性  
            private SftpClient sftp;  
            /// <summary>  
            /// SFTP连接状态  
            /// </summary>  
            public bool Connected { get { return sftp.IsConnected; } }  
            #endregion  
 
            #region 构造  
            /// <summary>  
            /// 构造  
            /// </summary>  
            /// <param name="ip">IP</param>  
            /// <param name="port">端口</param>  
            /// <param name="user">用户名</param>  
            /// <param name="pwd">密码</param>  
            public SFTPOperation(string ip, string port, string user, string pwd)  
            {  
                sftp = new SftpClient(ip, Int32.Parse(port), user, pwd);  
            }  
            #endregion  
 
            #region 连接SFTP  
            /// <summary>  
            /// 连接SFTP  
            /// </summary>  
            /// <returns>true成功</returns>  
            public bool Connect()  
            {  
                try  
                {  
                    if (!Connected)  
                    {  
                        sftp.Connect();  
                    }  
                    return true;  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("连接SFTP失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("连接SFTP失败,原因:{0}", ex.Message));  
                }  
            }  
            #endregion  
 
            #region 断开SFTP  
            /// <summary>  
            /// 断开SFTP  
            /// </summary>   
            public void Disconnect()  
            {  
                try  
                {  
                    if (sftp != null && Connected)  
                    {  
                        sftp.Disconnect();  
                    }  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("断开SFTP失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("断开SFTP失败,原因:{0}", ex.Message));  
                }  
            }  
            #endregion  
 
            #region SFTP上传文件  
            /// <summary>  
            /// SFTP上传文件  
            /// </summary>  
            /// <param name="localPath">本地路径</param>  
            /// <param name="remotePath">远程路径</param>  
            public bool Put(string localPath, string remotePath)  
            {  
                bool result = false;  
                try  
                {  
                    using (var file = File.OpenRead(localPath))  
                    {  
                        Connect();  
                        sftp.UploadFile(file, remotePath);  
                        Disconnect();  
                        result = true;  
                    }  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件上传失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("SFTP文件上传失败,原因:{0}", ex.Message));  
                }  
                return result;  
            }  
            #endregion  
 
            #region SFTP获取文件  
            /// <summary>  
            /// SFTP获取文件  
            /// </summary>  
            /// <param name="remotePath">远程路径</param>  
            /// <param name="localPath">本地路径</param>  
            public bool  Get(string remotePath, string localPath)  
            {  
                bool result = false;  
                try  
                {  
                    Connect();  
                    var byt = sftp.ReadAllBytes(remotePath);  
                    Disconnect();  
                    File.WriteAllBytes(localPath, byt);  
                    result = true;  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件获取失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("SFTP文件获取失败,原因:{0}", ex.Message));  
                }  
                return result;  
  
            }  
            #endregion  
 
            #region 删除SFTP文件  
            /// <summary>  
            /// 删除SFTP文件   
            /// </summary>  
            /// <param name="remoteFile">远程路径</param>  
            public void Delete(string remoteFile)  
            {  
                try  
                {  
                    Connect();  
                    sftp.Delete(remoteFile);  
                    Disconnect();  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件删除失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("SFTP文件删除失败,原因:{0}", ex.Message));  
                }  
            }  
            #endregion  
 
            #region 获取SFTP文件列表  
            /// <summary>  
            /// 获取SFTP文件列表  
            /// </summary>  
            /// <param name="remotePath">远程目录</param>  
            /// <param name="fileSuffix">文件后缀</param>  
            /// <returns></returns>  
            public ArrayList GetFileList(string remotePath, string fileSuffix)  
            {  
                try  
                {  
                    Connect();  
                    var files = sftp.ListDirectory(remotePath);  
                    Disconnect();  
                    var objList = new ArrayList();  
                    foreach (var file in files)  
                    {  
                        string name = file.Name;  
                        if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length))  
                        {  
                            objList.Add(name);  
                        }  
                    }  
                    return objList;  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("SFTP文件列表获取失败,原因:{0}", ex.Message));  
                }  
            }  
            #endregion  
 
            #region 移动SFTP文件  
            /// <summary>  
            /// 移动SFTP文件  
            /// </summary>  
            /// <param name="oldRemotePath">旧远程路径</param>  
            /// <param name="newRemotePath">新远程路径</param>  
            public void Move(string oldRemotePath, string newRemotePath)  
            {  
                try  
                {  
                    Connect();  
                    sftp.RenameFile(oldRemotePath, newRemotePath);  
                    Disconnect();  
                }  
                catch (Exception ex)  
                {  
                    //TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件移动失败,原因:{0}", ex.Message));  
                    throw new Exception(string.Format("SFTP文件移动失败,原因:{0}", ex.Message));  
                }  
            }  
            #endregion  
  
        }  
  
    }
类库的下载地址:http://download.csdn.net/detail/chengmin1989/9812647

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值