c#打开ftp服务器上的文件,【C#】FtpHelper对服务器文件实现读写操作的实例详解...

这篇文章主要为大家详细介绍了FtpHelper实现ftp服务器文件读写操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.IO;

using System.Threading;

using System.Configuration;

namespace FtpSyn

{

public class FtpHelper

{

//基本设置 ftp://400:[email protected]/400backup

static private string path = @"ftp://" + ConfigurationManager.AppSettings["FtpServerIP"].ToString() + "/"; //目标路径

static private string ftpip = ConfigurationManager.AppSettings["FtpServerIP"].ToString(); //ftp IP地址

static private string username = ConfigurationManager.AppSettings["FtpUserName"].ToString(); //ftp用户名

static private string password = ConfigurationManager.AppSettings["FtpPassWord"].ToString(); //ftp密码

//获取ftp上面的文件和文件夹

public static string[] GetFileList(string dir)

{

string[] downloadFiles;

StringBuilder result = new StringBuilder();

FtpWebRequest request;

try

{

request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + dir));

request.UseBinary = true;

request.Credentials = new NetworkCredential(username, password);//设置用户名和密码

request.Method = WebRequestMethods.Ftp.ListDirectory;

request.UseBinary = true;

request.UsePassive = false; //选择主动还是被动模式 , 这句要加上的。

request.KeepAlive = false;//一定要设置此属性,否则一次性下载多个文件的时候,会出现异常。

WebResponse response = request.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();

while (line != null)

{

result.Append(line);

result.Append("\n");

line = reader.ReadLine();

}

result.Remove(result.ToString().LastIndexOf('\n'), 1);

reader.Close();

response.Close();

return result.ToString().Split('\n');

}

catch (Exception ex)

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于C#FTP上传帮助类示例: ```csharp using System; using System.IO; using System.Net; public class FtpHelper { private string ftpServerIP; private string ftpUserID; private string ftpPassword; private FtpWebRequest reqFTP; public FtpHelper(string ftpServerIP, string ftpUserID, string ftpPassword) { this.ftpServerIP = ftpServerIP; this.ftpUserID = ftpUserID; this.ftpPassword = ftpPassword; } // 上传文件 public bool Upload(string localFilePath, string remoteFileName) { FileInfo fileInfo = new FileInfo(localFilePath); string uri = "ftp://" + ftpServerIP + "/" + remoteFileName; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInfo.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; using (FileStream fs = fileInfo.OpenRead()) { using (Stream strm = reqFTP.GetRequestStream()) { contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } } } FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } } ``` 使用方法: ```csharp string localFilePath = @"C:\test.txt"; string remoteFileName = "test.txt"; string ftpServerIP = "ftp://192.168.1.1"; string ftpUserID = "username"; string ftpPassword = "password"; FtpHelper ftpHelper = new FtpHelper(ftpServerIP, ftpUserID, ftpPassword); ftpHelper.Upload(localFilePath, remoteFileName); ``` 其中,`localFilePath`为本地文件路径,`remoteFileName`为远程文件名,`ftpServerIP`为FTP服务器地址,`ftpUserID`和`ftpPassword`为FTP登录凭据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值