.net操作sftp服务器
作者:网络转载 发布时间:[ 2015/4/22 14:14:01 ] 推荐标签:.net sftp服务器
97: /// <summary>
98: /// 删除SFTP文件
99: /// </summary>
100: /// <param name="remoteFile"></param>
101: /// <returns></returns>
102:
103: public void Delete(string remoteFile)
104: {
105: string fullRemotePath = defRemotePath + remoteFile.TrimStart('/');
106: m_sftp.rm(fullRemotePath);
107: }
108: /// <summary>
109: /// 获取SFTP文件列表
110: /// </summary>
111: /// <param name="remotePath"></param>
112: /// <param name="fileType">文件后缀名称(.txt)</param>
113: /// <returns></returns>
114: public List<string> GetFileList(string remotePath, string fileType)
115: {
116: List<string> objList = new List<string>();
117: string fullRemotePath = defRemotePath + remotePath.TrimStart('/');
118: if (DirExist(fullRemotePath))
119: {
120: Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(fullRemotePath);
121: foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv)
122: {
123: string sss = qqq.getFilename();
124: if (sss.Length > (fileType.Length + 1) && fileType == sss.Substring(sss.Length - fileType.Length))
125: { objList.Add(sss); }
126: else { continue; }
127: }
128: }
129: return objList;
130: }
131:
132: /// <summary>
133: /// 目录是否存在
134: /// </summary>
135: /// <param name="dirName">目录名称必须从根开始</param>
136: /// <returns></returns>
137: public bool DirExist(string dirName)
138: {
139: try
140: {
141: m_sftp.ls(defRemotePath + dirName.TrimStart('/'));
142: return true;
143: }
144: catch (Tamir.SharpSsh.jsch.SftpException)
145: {
146: return false;//执行ls命令时出错,则目录不存在。
147: }
148: }
149:
150: /// <summary>
151: /// 创建目录
152: /// </summary>
153: /// <param name="dirName">目录名称必须从根开始</param>
154: /// <returns></returns>
155: public void Mkdir(string dirName)
156: {
157: Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(defRemotePath);
158: foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry fileName in vvv)
159: {
160: string name = fileName.getFilename();
161: if (name == dirName)
162: {
163: throw new Exception("dir is exist");
164: }
165: }
166: m_sftp.mkdir(defRemotePath + dirName.TrimStart('/'));
167: }
168:
169: /// <summary>
170: /// 连接SFTP
171: /// </summary>
172: public void ConnectSftp()
173: {
174: JSch jsch = new JSch(); //利用java实现的通讯包
175: m_session = jsch.getSession(this.UserName, this.HostName, this.Port);
176: m_session.setHost(this.HostName);
177: MyUserInfo ui = new MyUserInfo();
178: ui.setPassword(this.Password);
179: m_session.setUserInfo(ui);
180:
181: if (!m_session.isConnected())
182: {
183: m_session.connect();
184: m_channel = m_session.openChannel("sftp");
185: m_channel.connect();
186: m_sftp = (ChannelSftp)m_channel;
187: }
188: }
189:
190: /// <summary>
191: /// 断开SFTP
192: /// </summary>
193: public void DisconnectSftp()
194: {
195: if (m_session.isConnected())
196: {
197: m_channel.disconnect();
198: m_session.disconnect();
199: }
200: }
201:
202: #endregion
203:
204: //登录验证信息
205: private class MyUserInfo : UserInfo
206: {
207: String passwd;
208: public String getPassword() { return passwd; }
209: public void setPassword(String passwd) { this.passwd = passwd; }
210:
211: public String getPassphrase() { return null; }
212: public bool promptPassphrase(String message) { return true; }
213:
214: public bool promptPassword(String message) { return true; }
215: public bool promptYesNo(String message) { return true; }
216: public void showMessage(String message) { }
217: }
218:
219: public void Dispose()
220: {
221: this.DisconnectSftp();
222: this.m_channel = null;
223: this.m_session = null;
224: this.m_sftp = null;
225: }
226: }
227:
228: }
配置文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="SftpServer" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<SftpServer>
<add key="host_name" value="127.0.0.1"/>
<add key="user_name" value="test"/>
<add key="password" value="123"/>
</SftpServer>
</configuration>
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
编程常用的几种时间戳转换(java .net 数据库).Net中关于相等的问题Asp.net MVC如何对所有用户输入的字符串字段做Trim处理Asp.Net WebForm生命周期的详解.Net开发的两个小技巧asp.net 六大内置对象.Net基础体系和跨框架开发普及Linux使用Jexus托管Asp.Net Core应用程序不被看好!苹果收购Netflix或将两败俱伤C#控制台程序使用Log4net日志组件asp.net登录验证码实现方法ASP.NET自带对象JSON字符串与实体类的转换从 .NET 和 Java 之争谈 IT 行业.Net高效开发之不可错过的实用工具ASP.NET MVC必须知道的那些事!Linux Netlink基本使用
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南