c#FTP基本使用
public class FtpHelper
{
//基本设置
private static string ftppath = @"ftp://" + "192.168.1.1" + "/";
private static string username = "username";
private static string password = "password"; //获取FTP上面的文件夹和文件
public static string[] GetFolderAndFileList(string s)
{
string[] getfolderandfilelist;
FtpWebRequest request;
StringBuilder sb = new StringBuilder();
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
sb.Append(line);
sb.Append("\n");
Console.WriteLine(line);
line = reader.ReadLine();
}
sb.Remove(sb.ToString().LastIndexOf('\n'), );
reader.Close();
response.Close();
return sb.ToString().Split('\n');
}
catch (Exception ex)
{
Console.WriteLine("获取FTP上面的文件夹和文件:" + ex.Message);
getfolderandfilelist = null;
return getfolderandfilelist;
}
} //获取FTP上面的文件大小
public static int GetFileSize(string fileName)
{
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath + fileName));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
int n = (int)request.GetResponse().ContentLength;
return n;
}
catch (Exception ex)
{
Console.WriteLine("获取FTP上面的文件大小:" + ex.Message);
return -;
}
} //FTP上传文件
public static void FileUpLoad(string filePath, string objPath = "")
{
try
{
string url = ftppath;
if (objPath != "")
url += objPath + "/";
try
{
FtpWebRequest request = null;
try
{
FileInfo fi = new FileInfo(filePath);
using (FileStream fs = fi.OpenRead())
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fi.Name));
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
using (Stream stream = request.GetRequestStream())
{
int bufferLength = ;
byte[] buffer = new byte[bufferLength];
int i;
while ((i = fs.Read(buffer, , bufferLength)) > )
{
stream.Write(buffer, , i);
}
Console.WriteLine("FTP上传文件succesful");
}
}
}
catch (Exception ex)
{
Console.WriteLine("FTP上传文件:" + ex.Message);
}
finally
{ }
}
catch (Exception ex)
{
Console.WriteLine("FTP上传文件:" + ex.Message);
}
finally
{ }
}
catch (Exception ex)
{
Console.WriteLine("FTP上传文件:" + ex.Message);
}
} //FTP下载文件
public static void FileDownLoad(string fileName)
{
FtpWebRequest request;
try
{
string downloadPath = @"D:";
FileStream fs = new FileStream(downloadPath + "\\" + fileName, FileMode.Create);
request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftppath + fileName));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
int bufferLength = ;
int i;
byte[] buffer = new byte[bufferLength];
i = stream.Read(buffer, , bufferLength);
while (i > )
{
fs.Write(buffer, , i);
i = stream.Read(buffer, , bufferLength);
}
stream.Close();
fs.Close();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP下载文件:" + ex.Message);
}
} //FTP删除文件
public static void FileDelete(string fileName)
{
try
{
string uri = ftppath + fileName;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP删除文件:" + ex.Message);
}
} //FTP新建目录,上一级须先存在
public static void MakeDir(string dirName)
{
try
{
string uri = ftppath + dirName;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
request.UseBinary = true;
request.Credentials = new NetworkCredential(username, password);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP新建目录:" + ex.Message);
}
} //FTP删除目录,上一级须先存在
public static void DelDir(string dirName)
{
try
{
string uri = ftppath + dirName;
FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(username, password);
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
Console.WriteLine("FTP删除目录:" + ex.Message);
}
}
}
c#FTP基本使用的更多相关文章
- 8.仿阿里云虚拟云服务器的FTP(包括FTP文件夹大小限制)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html#iis 原文:http://dnt.dkill.net/Ar ...
- Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...
- 阿里云学生优惠Windows Server 2012 R2安装IIS,ftp等组件,绑定服务器域名,域名解析到服务器,域名备案,以及安装期间错误的解决方案
前言: 这几天终于还是按耐不住买了一个月阿里云的学生优惠.只要是学生,在学信网上注册过,并且支付宝实名认证,就可以用9块9的价格买阿里云的云服务ECS.确实是相当的优惠. 我买的是Windows S ...
- win7下利用ftp实现华为路由器的上传和下载
win7下利用ftp实现华为路由器的上传和下载 1. Win7下ftp的安装和配置 (1)开始->控制面板->程序->程序和功能->打开或关闭Windows功能 (2)在Wi ...
- Java实现FTP文件与文件夹的上传和下载
Java实现FTP文件与文件夹的上传和下载 FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制 ...
- centos下开启ftp服务
如果要ftp访问linux需要安装ftp服务,vsftpd是Linux下比较好的的FTP服务器. 一.检查安装vsftp //检查是否安装vsftpd rpm -qa | grep vsftpd // ...
- 解决开启服务器防火墙导致ftp不能连接的问题
在防火墙设置的"高级"选项卡中的"网络连接设置"--"本地连接"--"设置"中添加了"FTP服务器" ...
- centos6.5 nginx-1.8.0和ftp搭建图片服务器
一.Nginx的安装步骤 1.Nginx安装环境: gcc: 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c+ ...
- Jenkins配置MSBuild实现自动部署(MSBuild+SVN/Subversion+FTP+BAT)
所要用到的主要插件: [MSBuild Plugin] 具体操作: 1.配置MSBuild的版本 [系统管理]->[Global Tool Configuration]->[MSBuild ...
- [CentOs7]搭建ftp服务器(2)——添加用户
摘要 上篇文章完成了ftp服务器的安装与匿名访问的内容,当然出于安全的考虑是不允许匿名访问服务器的,所以就有了本篇的内容 ,为ftp服务器添加用户,用改用户进行访问. vsftpd添加用户 FTP用户 ...
随机推荐
- web语义化这个坑
什么是wen语义化:https://www.zhihu.com/question/20455165 标签大全:http://www.w3school.com.cn/tags/tag_html.asp ...
- Powershell下git中文乱码
问题 使用git log查看提交历史, 发现中文的部分出现了乱码, 如图 解决方案 powershell中输入下面的命令 git config --global core.quotepath fals ...
- css: line-height 与box-sizing
css 1.line-hight: 行高line-height,是文本行基线这件的距离,不是字体大小,它确定了各个元素框的高度增加或减少多少. 对于块级元素:定义了元素中文本基线之间的最小距离. li ...
- ssh_exchange_identification: read: Connection reset
垃圾服务器,真的佛了,明明服务器从装的系统,连接半天连接不上,但是别人的电脑就可以,要使用xshell和fileshell链接,按照软件报的错误来修复的话,根本解决不了问题,还是得命令行ssh roo ...
- [Effective Java 读书笔记] 第8章 通用程序设计
本章主要讲了以下几条基本的JAVA编程原则: 1.将局部变量的作用域控制在最小,在使用时才定义 2.for-each优于for循环 有三个例外(1,2点主旨就是,for each只能用于读取,不能用于 ...
- 一个基于图的数据管理系统-gStore
gStore是遵循 BSD协议的一个开源项目.一个基于图的 RDF 三元组存储的数据管理系统.该项目是北京大学.滑铁卢大学.香港科技大学的联合研究项目.中国北京大学计算机科学与技术研究所的数据库组对该 ...
- 线段树学习----C语言
/* 线段树学习:如果一个节点为i,那么他的左孩子为2I+1,右孩子为2i+2: */ #include<stdio.h> #define min(a,b) a<b?a:b; ]; ...
- 使用Webpack的代码拆分在Vue中进行懒加载
参考学习:https://alexjover.com/blog/lazy-load-in-vue-using-webpack-s-code-splitting/ 学习文案:https://webpac ...
- webpack 中那些最易混淆的 5 个知识点
学习博客:https://blog.csdn.net/wsyzxxn9/article/details/90677770 学习lodash:https://www.html.cn/doc/lodash ...
- iTerm 2 与 oh-my-zsh配合,自定义你的终端。
参考博客:https://www.cnblogs.com/xishuai/p/mac-iterm2.html 参考博客:https://www.cnblogs.com/sasuke6/p/497607 ...