摘要:本文主要向大家介绍了C#编程之C#实现ftp上传和下载,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之C#实现ftp上传和下载,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
调整内容为三部分:Web.config配置,帮助类,实现调用第一步:Web.config配置如下:
第二步:帮助类FtpHelper如下:
public class FtpHelper
{
public static string _host = ConfigurationManager.AppSettings["FtpUrl"];
public static string _userId = ConfigurationManager.AppSettings["FtpUserID"];
public static string _password = ConfigurationManager.AppSettings["FtpPwd"];
public static string _remotePath = ConfigurationManager.AppSettings["FtpRemotePath"];
#region 构造函数
///
///
主机名称
///
用户名
///
密码
public FtpHelper()
: this(_host, _userId, _password, 21, null, false, true, true)
{
}
///
///
主机名称
///
用户名
///
密码
///
端口
///
允许Ssl
///
代理
///
允许二进制
///
允许被动模式
public FtpHelper(string host, string userId, string password, int port, IWebProxy proxy, bool enableSsl, bool useBinary, bool usePassive)
{
this.userId = userId;
this.password = password;
if (host.ToLower().StartsWith("ftp://"))
{
this.host = host;
}
else
{
this.host = "ftp://" + host;
}
this.port = port;
this.proxy = proxy;
this.enableSsl = enableSsl;
this.useBinary = useBinary;
this.usePassive = usePassive;
}
#endregion
#region 变量
#region 主机
private string host = string.Empty;
///
public string Host
{
get
{
return this.host ?? string.Empty;//如果左操作数为空则返回右操作数,不为空返回左操作数
}
}
#endregion
#region 登录用户名
private string userId = string.Empty;
///
public string UserId
{
get
{
return this.userId;
}
}
#endregion
#region 密码
private string password = string.Empty;
///
public string Password
{
get
{
return this.password;
}
}
#endregion
#region 代理
IWebProxy proxy = null;
///
public IWebProxy Proxy
{
get
{
return this.proxy;
}
set
{
this.proxy = value;
}
}
#endregion
#region 端口
private int port = 21;
///
public int Port
{
get
{
return port;
}
set
{
this.port = value;
}
}
#endregion
#region 设置是否允许Ssl
private bool enableSsl = false;
///
public bool EnableSsl
{
get
{
return enableSsl;
}
}
#endregion
#region 使用被动模式
private bool usePassive = true;
///
public bool UsePassive
{
get
{
return usePassive;
}
set
{
this.usePassive = value;
}
}
#endregion
#region 二进制方式
private bool useBinary = true;
///
public bool UseBinary
{
get
{
return useBinary;
}
set
{
this.useBinary = value;
}
}
#endregion
#region 远端路径
private string remotePath = "/" + _remotePath + "/";
///
public string RemotePath
{
get
{
return remotePath;
}
set
{
string result = "/";
if (!string.IsNullOrEmpty(value) && value != "/")
{
result = "/" + value.TrimStart(‘/‘).TrimEnd(‘/‘) + "/";
}
this.remotePath = result;
}
}
#endregion
#endregion
#region 创建一个FTP连接
///
///
请求地址
///
请求方法
///
private FtpWebRequest CreateRequest(string url, string method)
{
//建立连接
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
request.Credentials = new NetworkCredential(this.userId, this.password);
request.Proxy = this.proxy;
request.KeepAlive = false;//命令执行完毕之后关闭连接
request.UseBinary = useBinary;
request.UsePassive = usePassive;
request.EnableSsl = enableSsl;
request.Method = method;
return request;
}
#endregion
#region 上传一个文件到远端路径下
///
///
本地文件信息
///
要保存到FTP文件服务器上的文件名称包含扩展名
public bool Upload(FileInfo localFile, string remoteFileName)
{
bool result = false;
if (localFile.Exists)
{
string url = Host.TrimEnd(‘/‘) + RemotePath + remoteFileName;
FtpWebRequest request = CreateRequest(url, WebRequestMethods.Ftp.UploadFile);
//上传数据
using (Stream rs = request.GetRequestStream())
using (FileStream fs = localFile.OpenRead())
{
byte[] buffer = new byte[4096];//4K
int count = fs.Read(buffer, 0, buffer.Length);//每次从流中读4个字节再写入缓冲区
while (count > 0)
{
rs.Write(buffer, 0, count);
count = fs.Read(buffer, 0, buffer.Length);
}
fs.Close();
result = true;
}
return result;
}
throw new Exception(string.Format("本地文件不存在,文件路径:{0}", localFile.FullName));
}
#endregion
#region 从FTP服务器上下载文件
///
///
服务器上的文件名称
///
本地文件名称
///
public bool Download(string serverName, string localName)
{
bool result = false;
using (FileStream fs = new FileStream(localName, FileMode.OpenOrCreate)) //创建或打开本地文件
{
//建立连接
string url = Host.TrimEnd(‘/‘) + RemotePath + serverName;
FtpWebRequest request = CreateRequest(url, WebRequestMethods.Ftp.DownloadFile);
request.ContentOffset = fs.Length;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
fs.Position = fs.Length;
byte[] buffer = new byte[4096];//4K
int count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
while (count > 0)
{
fs.Write(buffer, 0, count);
count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
}
response.GetResponseStream().Close();
}
result = true;
}
return result;
}
#endregion
#region 重命名FTP服务器上的文件
///
///
原文件名
///
新文件名
///
public bool Rename(string oldFileName, string newFileName)
{
bool result = false;
//建立连接
string url = Host.TrimEnd(‘/‘) + RemotePath + oldFileName;
FtpWebRequest request = CreateRequest(url, WebRequestMethods.Ftp.Rename);
request.RenameTo = newFileName;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
result = true;
}
return result;
}
#endregion
#region 从当前目录下获取文件列表
///
///
public List
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号